I have an array of strings and I am populating a table view with the array data. I want the table view to display 1 in the first row, 2 in the second row, 3 in the third row. I have done this before but it is not working now. Every time I run the app, it displays the number of elements in the array in every row. What am I doing wrong?
func numPick() -> Int {
var num: Int = 0
for _ in myRoster! {
num+=1
}
return num
}
I have an array of strings
I gather it is myRoster ?
Please, show how you use it in tableView to populate cells.
If I understand correctly what you want, what you should simply do:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CellID", for: indexPath)
// Configure the cell...
cell.textLabel!.text = "\(indexPath.row+1)" + myRoster![indexPath.row]
return cell
}
I don't understand what numPick() is used for.
The way you compute, it just returns the number of items in myRoster (you add 1 to num for each item in myRoster!), so necessarily the same value in all cases.
for _ in myRoster! {
num+=1
}
maybe what you intended was to count till a certain position that you would pass as argument ?
but that would be equivalent to return the input parameter (may be incremented by 1). That would also be totally useless.