Reputation: 2705
I have an UITableView where all my cells have an UISwitch as an accessoryView.
I also have an array of all my UISwitches.
At the viewDidAppear method I iterate through my array of UISwitches and disable them.
It is all working fine.
The problem is when I move the UITableView up and down and the UISwitches that get hidden and shown again are not dimmed. They are disabled as intended but they look like they are enabled.
How can I resolve that?
Upvotes: 0
Views: 356
Reputation: 10520
The problem is your cell are getting reused, and therefore the cell switches are getting reused. One solution, is to force your cells to not get reused ie:
MyCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
//if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"MyCustomCell" owner:nil options:nil];
for(id currentObject in topLevelObjects){
if([currentObject isKindOfClass:[UITableViewCell class]]){
cell = (MyCustomCell *)currentObject;
break;
}
}
//}
Notice that the if cell==nil is commented out, so the cells are never reused. Obviously not great for conserving memory and such, but if you don't have a large or weird tableview, you probably wouldn't even notice.
Another thing you could try is keep an array of boolean values that determine whether or not the switch should be checked, and use that array in the cellForRowAtIndexPath
method.
Upvotes: 2