Context: I have a tableview with custom cells. Each cell has a button to download a resource. When a button is pressed all other buttons in the tableview should be disabled.
So my problem is that I can’t get a reference to all the rows on the tableview since it is lazy loading. So when I try to access the other rows it fails because it doesn’t have an object to add at the point where I start disabling the buttons.
Here is the code it may be more enlightening on my problem:
NSMutableArray *cells = [NSMutableArray array];
for (NSInteger i = 0; i < [mainTableView numberOfRowsInSection:1]; ++i) {
WSVideoLibraryTableCell *addedCell = (WSVideoLibraryTableCell *)[mainTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:1]];
NSLog(@"CELL ADDED: %@", addedCell);
[cells addObject:addedCell];
}
if (stateEnabled) {
for (WSVideoLibraryTableCell *cell in cells) {
[[cell downloadButton] setEnabled:YES];
}
}
else
{
for (WSVideoLibraryTableCell *cell in cells) {
[[cell downloadButton] setEnabled:NO];
}
}
I tried adding some code that takes only the visible cells. NSArray *array = [mainTableView visibleCells] but then the problem is that the cells that weren’t visible will have the button enabled and something weird happens with the rows that where visible, they appear again and again on the tableView (Something to do with lazy loading and changing them?)
Thanks a lot for comments or suggestions!!
My suggestion would be to have each object have its own
isEnabledproperty, so that you can disable the objects themselves, and set a condition for the lazy loading to disable them according to that property.Lazy loading is very helpful, and while for a trivial number of cells you won’t notice a resource drain, you will as you increase the number of cells you’re processing.