I am facing Index out of range issue in UITableView.
Below is my code :
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 50
}
//== Teble: number of tables ==============================//
func numberOfSections(in tableView: UITableView) -> Int {
return headerTitleArray.count
}
//== Teble rows: number of rows ==============================//
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return cellTitleMultiArray.count
}
//== Teble rows: data for each row ==============================//
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCell", for: indexPath) as! CustomTableViewCell
let text = cellTitleMultiArray[indexPath.section][indexPath.row]
cell.titleLabel.text = text
return cell
}
}
I am trying to solve this issue but couldn't .Please please help me solve this issue....
It looks like your cellTitleMultiArray is an array of arrays, where the outer array contains an entry for each section, and for each section, the inner array at that index contains entries for each row in that section.
Thus numberOfSections(in:) should probably return the number of entries in cellTitleMultiArray.
tableView(_:numberOfRowsInSection:) should probably return the number of entries in the cellTitleMultiArray sub-array at the current section. See if you can work out that bit of code.
Related
Hello, Im trying to automatically modify number of rows in my tableView to = the count in my Array. The data works fine and number of rows loads accordingly. However the table itself always shows 4 rows, it won't load more and won't show less even when less are loaded, just as you can see in the picture. Below is my script and settings.
extension FirstViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("click")
}
}
extension FirstViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// let a = UserDefaults.standard.integer(forKey: "RunwayRows")
// return a
return runways.count
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = runways[indexPath.row].ident1
print(runways.count)
return cell
}
}
I have the following situation. I am making a weather app and I am displaying the data in cells for a period of days. I have the following structure:
Day1
Min temperature
Max temperature
Day2
Min temperature
Max temperature
Day3
Min temperature
Max temperature
etc etc...
I am also using sections and the following functions:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCell(withIdentifier: "minimumTemperature")
cell?.textLabel?.text = "fooMinTemperature"
return cell!
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 2
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 10
}
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return // logic for getting the heading of the section
}
The problem comes that the override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
function can only return 1 cell at a time. So for example this function will get called two times now and add the minimum temperature two times. How can I add both the min and max temperatures.
Assuming you have some array of data where each element in the array represents one day, you would use the section to pick the data for the correct day. Then you use the row to choose between min or max temperature.
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCell(withIdentifier: "minimumTemperature") as! UITableViewCell
let data = myArrayOfDays[indexPath.section]
let temp = indexPath.row == 0 ? data.minimumTemp : data.maximumTemp
cell.textLabel?.text = "\(temp)"
return cell
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 2
}
override func numberOfSections(in tableView: UITableView) -> Int {
return myArrayOfDays.count
}
The variable names I used are clearly just examples. Update with your own as needed.
Your section header can show the day number.
Whenever I run the app, the tableView has no data, waiting for user to input. The problem is that if the numberOfSections is 1, it works just fine, but when I change it to 2 it crashes because Index out of range
func numberOfSections(in tableView: UITableView) -> Int {
return 2
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "expenseCell") as? ExpenseCell else { return UITableViewCell() }
let budget = userBudget[indexPath.section][indexPath.row]
cell.delegate = self
cell.configureCell(budget: budget)
return cell
}
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle {
return UITableViewCellEditingStyle.none
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return userBudget[section].count // <- Crash here
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return "Practice with Sections \(section)"
}
It is because you are accessing index 1 of userBudget while i assume it contains 0 index only.
This crash happens cause your array userBudget, don't has two elements at the moment you try access his second position.
You must guard that userBudget has two elements on minimium...
You should that you must to assign value to userBudget on your ViewDidLoad.
You are saying that you will have two section to your tableView's delegate, but you have an array which contains only one array. Basically when you try to reach userBadget[1] in your numberOfRowsInSection function and it crashes because it doesn't exist.
Replace
func numberOfSections(in tableView: UITableView) -> Int {
return 2
}
With
func numberOfSections(in tableView: UITableView) -> Int {
return userBudget.count
}
I am trying to create a tableView for each value in my array and I am not sure how to go about creating that. The code below clearly doesn't work I just used it to illustrate my point. How do I differentiate between tables in the tableView protocol functions.
let array = ["1","2","3","4","5"]
for value in array{
let newTableView: UITableView()
//Continue with creation of table
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.tableContent.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
return cell
}
Any help is appreciated and please comment if the question is unclear
Previously, I have needed multiple TableViews or Collection Views in a single ViewController, e.g. for different tabs.
You can identify each table by using the tag property.
let data : [Int : [YourObjects]]
for key, value in data {
let newTableView: UITableView()
// Identify the table
newTableView.tag = key
newTableView.dataSource = self
//Continue with creation of table
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.data[tableView.tag].count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// Dequeue cell
// Get the data for this table, identified by the tag
let tableData = data[tableView.tag]
cell.populateCell(withData: tableData[indexPath.row])
return cell
}
I put Table in my ViewController. I made GroupTable. So, I select the table and choose "Grouped" in style in Attribute Inspector.
var TableArray = [["Home","Apple","Orange","Pineapple"],["Flour","Cake Flour"]]
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return TableArray.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
debugPrint(TableArray[section].count)
return TableArray[section].count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: TableArray[indexPath.section][indexPath.row], for: indexPath) as UITableViewCell
cell.imageView?.image = menuIconImage[indexPath.row]
cell.textLabel?.text = TableArray[indexPath.section][indexPath.row]
cell.selectionStyle = UITableViewCellSelectionStyle.blue
tableView.rowHeight = 56.0;
return cell
}
When I run the app, the table only shows "Home","Apple","Orange","Pineapple". It does not show "Flour", "Cake Flour".
I don't know where the problem is. Please help me.
The method numberOfSectionsInTableView is wrong, the default value is 1.
The Swift 3 syntax is:
func numberOfSections(in tableView: UITableView) -> Int {
return TableArray.count
}
According to the naming convention variable names should start with a lowercase letter.