[swift-users] Issues with UITableView*

Adam Stoller ghoti1 at gmail.com
Mon Sep 12 17:14:34 CDT 2016


Hmm - interesting to know.  Unfortunately, if I do that, then I get NO indication of selection when I click on ANY row.  Perhaps I need to make some other changes to account for the change in how I get the cell?

—fish
(Adam Stoller)

> On Sep 12, 2016, at 18:08, Ole Begemann <ole at oleb.net> wrote:
> 
> > 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 `UITableViewDataSource`.
> 
> Hope this helps,
> Ole
> 



More information about the swift-users mailing list