Michael
Michael

Reputation: 577

Cost of iterating over cells in a UITableView

Inside each UITableViewCell of my UITableView, I have a UIScrollView. The scroll view is setup so that when the user swipes right a menu will appear. This is similar to the behavior of the cells in the iPhone Twitter app. When a user swipes upon another cell I iterate over all visible cells to tell the UIScrollView to scroll back to the cell content (i.e. its initial position). The iteration is done in the scrollViewWillBeginDragging method with the following code:

   - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {

        if( [scrollView tag] == 90 ) {

            NSLog(@"Dragging a scroll view inside a cell!");

            for (UITableViewCell *cell in self.tableView.visibleCells) {

                [(UICellContentScrollView *)[cell viewWithTag:90] scrollRectToVisible:CGRectMake(0.0f, 0.0f, 320.0f, [cell frame].size.height) animated:YES];

            }

        }

    }

In the method viewDidDisappear I iterate again over all cells to reset various things like so:

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];


    for( NSUInteger section = 0; section < [[self tableView] numberOfSections]; section++ ) {

        for( NSUInteger row = 0; row < [[self tableView] numberOfRowsInSection:section]; row++ ) {

            UITableViewCell *cell = [[self tableView] cellForRowAtIndexPath:[NSIndexPath indexPathForRow:row inSection:section]];
             // resetting cell here

        }

    }

}

My question is if I am (a) going about this the right way and (b) does anyone have any recommendations on a better solution considering the table view may be storing up 50 (no more than 100) items.

Upvotes: 1

Views: 216

Answers (1)

Jack Lawrence
Jack Lawrence

Reputation: 10782

Check out the NSNotification documentation. You could register all of your UITableViewCell objects to receive a notification you could call something like "cellWasSwiped" or "needToResetCells" or whatever. Then whenever you want to reset the cells you just post the notification. All of your UITableViewCell objects that are registered to receive it will get the notification and can then call whatever method you need.

Upvotes: 1

Related Questions