1

I'm curious what happens or if it's bad if you do not include the for: indexPath when creating a UITableViewCell. For example, here is the standard creation of a cell taken from a basic UITableViewController:

let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier", for: indexPath)
        

I'm curious if there would be any negative affects from creating a cell like so:

let cellWithoutIndexPath = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier")
StonedStudio
  • 507
  • 3
  • 20
  • 3
    Already asked [here](https://stackoverflow.com/a/36000801/14351818), [here](https://stackoverflow.com/questions/25826383/when-to-use-dequeuereusablecellwithidentifier-vs-dequeuereusablecellwithidentifi), and [here](https://stackoverflow.com/questions/44213804/ios-what-is-a-difference-between-dequeuereusablecellwithidentifierfor-and-d) – aheze Oct 08 '20 at 00:25

1 Answers1

0

To my knowledge, if you don't include the indexPath there is no way for the cell to know where to go next or what to do. The documentation description for 'IndexPath' is:

"A list of indexes that together represent the path to a specific location in a tree of nested arrays."

If I had an array of fruit that I wanted to populate the tableView with: [apple, orange, pear], the 'indexPath.row' would point out that the first cell should be at location 0 in the array and the second cell should be at location 1, etc.

Another example would be this line of code:

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    tableView.deselectRow(at: indexPath, animated: true)

Had I not included the indexPath, the tableView would not know which cell I want to deselect.