I am working on an app in which I want to toggle the status of some devices on/off by changing the image view of the cell. I have my devices in a table view and I am setting the editing mode of the table view on a button click and it is editing fine, but the problem is that it only changes the image of the cells which are currently visible, whether or not I have previously selected some of the rows which are not visible right now. Here is my code. Tell me what I am missing or what I should do to change the selected rows which are not visible.
- (void)viewDidLoad{
deviceTableVIew.rowHeight = 72.0;
[deviceTableVIew setAllowsSelectionDuringEditing:YES];
[deviceTableVIew setAllowsMultipleSelectionDuringEditing:YES];
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"mainBackground.jpg"]];
btnControl.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"btn-conrol.png"]];
[super viewDidLoad];
}
-(IBAction)control:(id)sender{
btnControl.enabled = false;
btnControl.hidden = true;
btnCancel.enabled = true;
btnCancel.hidden = false;
stateToggleToolbar.hidden = false;
[self.deviceTableVIew setEditing:YES animated:YES];
}
-(IBAction)setDevicesOn:(id)sender{
NSArray *paths = [self.deviceTableVIew indexPathsForSelectedRows];
for (NSIndexPath *path in paths) {
UITableViewCell *cell = (UITableViewCell *)[self.deviceTableVIew cellForRowAtIndexPath:path];
cell.imageView.image = [UIImage imageNamed:@"device-on-image.png"];
}
controlStatus = NO;
btnControl.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"btn-conrol.png"]];
stateToggleToolbar.hidden = true;
}
(I assume you have an array of custom objects or dictionaries somewhere that holds the information to populate your cells.)
A better strategy is to have your setDevicesOn: method update that data model to indicate what image you wish to display and, when it’s finished, call reloadData. Then, when cellForRowAtIndexPath: is called, you can set the cell’s actual image based on that data element.