views:

14

answers:

1

Hello,

I'm working on an iPhone application. This is a tab bar application, containing a navigation view controller within one particular tab. I have a table view in the navigation view controller. In this groupe table, I need to add some UISwitch to some particular items. for instance, I have the following sections:

  • accounts
    • twitter (&)
    • facebook (&)
    • linkedIn
  • publication
    • twitter (*)
    • facebook (*)
    • linkedIn (*)
  • connection
    • stay connected (*)
  • about
    • about

I need to have the UISwitch for the ones with the * but not for the other ones. I manage to add the UISwitch for thoses ones using condition on indexPath in the tableView:cellForRowAtIndexPath methods. BUT... when I scroll the view... the UISwitch are added to some other items (the one with the & above).

Could you please help ?

Thanks a lot,

Luc

+1  A: 

It almost certainly is a problem with your code for cell reuse.

When you dequeue a cell to reuse it, it is not reset to a fresh state, instead it still has a UISwitch if a UISwitch was added to it before.

So whenever you dequeue a cell for reuse, you will need to assume that it might have a UISwitch subview, and remove that subview if you don't want it before you return the cell.

Jon Rodriguez
ok, I understand. So basically a [cell removeSubview] should do the trick ? I also read about making our own UITableViewCell class, what would be the best ?
Luc
@Luc's 1st question: Yes. You can either a) remove the subview no matter what and then add it again if you want it (slow but easy to code), or b) check whether it's there and remove it if you don't want it.
Jon Rodriguez
@Luc's 2nd question: Subclassing UITableViewCell is a good idea. It enables proper OO design by allowing you to put the code that determines the cell's appearance within the cell's own class (rather than within the UITableViewController).
Jon Rodriguez
Thanks a lot for your explanation. I will go with 1 for a quick workaround and move to 2 then :-)
Luc