Home > Net >  Swift Firestore UITableView cellForRowAt downloadURL wrong cell in closure
Swift Firestore UITableView cellForRowAt downloadURL wrong cell in closure

Time:09-26

Why is it that inside of a UITableView cellForRowAt that the cell reference is getting messed up when calling: (cell is dequed from

guard let cell = tableView.dequeueReusableCell(withIdentifier: "venuepostcell")as? VenuePostTableViewCell else { return UITableViewCell()}

cell.tag = indexPath.row

sRef.downloadURL { downloadURL, error in
    if downloadURL != nil {
        print ("=== A \(indexPath.row)  \(cell.tag)")
        }
    }

cell.tag should always equal indexPath.row.

Initially, it does, but if I do a refresh at times indexPath.row does NOT equal cell.tag???

Initial

===  3  3
===  0  0
===  1  1
===  2  2
===  4  4

and yet on refresh it will come back as

===  0  0
===  1  1
===  4  0 <-- WRONG
===  4  4
===  3  3
===  3  1 <-- WRONG
===  2  2
===  1  3 <-- WRONG
===  0  4 <-- WRONG
===  4  0 <-- WRONG

CodePudding user response:

This is how the UITableView is implemented. sRef.downloadURL is an asynchronous call, which means that for the time the callback is received there is a possibility that the user scrolled the table view and a brand new tag is set for the cell, while the indexPath.row was unchanged.

You may have to change the approach by which you are implementing this.

  • Related