[swift-users] Issues with UITableView*

Ole Begemann ole at oleb.net
Mon Sep 12 17:08:42 CDT 2016

 > The current version of my code seems to satisfy points #1-3, but crashes
 > when attempting to deal with #4 - and I’m not sure that what I’ve done
 > is “good” or “correct” in terms of satisfying the first three points.

I haven't looked at all of your code, but one thing that could 
definitely cause problems is that you're calling 
`dequeueReusableCellWithIdentifier` from 
`tableView(_:didDeselectRowAtIndexPath:)` and 
`tableView(_:didSelectRowAtIndexPath`). Don't do that.

`dequeueReusableCellWithIdentifier` creates a new cell (or fetches an 
unused one from the table view's reuse pool). You should only ever call 
it from inside `tableView(_:cellForRowAtIndexPath:)`.

In `didSelectRow...` and `didDeselectRow...`, you want to ask the table 
view for the _existing_ cell at the specified index path. Do this with 
something like:

     if let cell = tableView.cellForRowAtIndexPath(indexPath) {
         // cell found, do something with it
     } else {
         // No cell exists at this index path.
         // This probably doesn't happen in these callbacks,
         // but you never know.

Note that while `tableView.cellForRowAtIndexPath(indexPath)` looks very 
much like the data source method you implemented above, it is a 
different method. This one is a method on `UITableView`, not 

Hope this helps,

More information about the swift-users mailing list