views:

128

answers:

0

All:

I have been fighting with an issue which has been bugging me for the past few days.

I have a UIPageControl and in each page I have a UITableView. Each tableView can have upto 4 cells. A user can make a selection, go to next page and select something else. I want to retain the selection in each page, so the user can change his/her mind anytime. So selection in each page is independent.

The way I implemented for efficiency is to reuse my cells, if not selected. Here is the relevant code:


- (UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"%s", __FUNCTION__);
    static NSString * ChoiceTableCellIdentifier = @"ChoiceTableCellIdentifier";
    choiceTableCell = (ChoiceTableCell *)[theTableView dequeueReusableCellWithIdentifier:ChoiceTableCellIdentifier]; 
    NSIndexPath* path = [self.tableView indexPathForSelectedRow];
    if( (choiceTableCell == nil) || (path) )
    {
     choiceTableCell = [[[ChoiceTableCell alloc] 
          initWithFrame:CGRectZero
          reuseIdentifier:ChoiceTableCellIdentifier] autorelease];
    }
    return choiceTableCell;
}

I thought if a cell is already selected (checking using NSIndexPath), I will create a new cell. However, I see the selections in the subsequent pages.

Also, I am reusing the page as well - are their any dependencies between UIPageControl and tableView in the page I should be aware of?

Thanks a lot for you help, Amy