How do I iterate through an array to add 1 to the variable every time the loop runs?

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
    }

Accepted Reply

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.

Replies

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.

Yes, it worked by + 1 to indexPath.row in cellForRowAt.


So I don't have to run a for loop to add 1 to the row every time?


I used numPick() as a function to hold the integer for the number of row.


This is what my table view looks like:


func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath as IndexPath)
        
        cell.textLabel?.text = "\(numPick())" + (myRoster?[indexPath.row])!
        cell.textLabel?.adjustsFontSizeToFitWidth = true
        cell.textLabel?.font = UIFont.systemFont(ofSize: 22)
        
        return cell
    }

So I don't have to run a for loop to add 1 to the row every time?


No, you don't need.

And in could not work that way: numPick() has no information on which row it applies to. So, it could not return a value speific to this row.


Wish you good continuation.