Iterate over custom UITableViewCells
UITableViews on the iPhoneâ„¢ often contain a variable number of cells. The following code snippet should be helpful for those who need to iterate over a variable length table and subsequently read or set data model and cell state
//loop through cells in each section and make sure the data model is in sync
NSInteger numSections = [self numberOfSectionsInTableView:someTable];
for (NSInteger s = 0; s < numSections; s++) {
NSLog(@"Section %d of %d", s, numSections);
NSInteger numRowsInSection = [self tableView:someTable numberOfRowsInSection:s];
for (NSInteger r = 0; r < numRowsInSection; r++) {
NSLog(@"Row %d of %d", r, numRowsInSection);
MyCell *cell = (MyCell *)[someTable cellForRowAtIndexPath:
[NSIndexPath indexPathForRow:r inSection:s]];
DataModelObject *theObject = (DataModelObject *)[fetchedResultsController objectAtIndexPath:
[NSIndexPath indexPathForRow:r inSection:s]];
[theObject setValue:[NSNumber numberWithBool:MyCell.someProperty] forKey:@"someEntityKey"];
}
}
Apple, the Apple logo, iPod, iPod touch, and iTunes are trademarks of Apple Inc., registered in the U.S. and other countries. iPhone is a trademark of Apple Inc. App Store is a service mark of Apple Inc.
