views:

343

answers:

2

Hi, i have custom cell with 2 buttons(the function of these buttons is just to disable the button that was pressed). When i use dequeueReusableCellWithIdentifier in this classic way:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
cell = ((MainCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]);
 if (cell == nil) {
[[NSBundle mainBundle] loadNibNamed:@"MainCell" owner:self options:nil];
 }
    return cell;
}

the UITableView has 1 section, the problem is: on the first cell when i pressed the button to disabled it and than scroll down to show other cells, when i scroll up again the first cell is a new cell and the button is enabled. I know that reuseIdentifier is used to no recreated a cell if was already created, but in this way i lost all info of the cells that are not more visible.

Any idea?

Thanks in advance

+3  A: 

I'm having similar issues--I think the problem is that only the visible cells are actually in memory at any given time, and when it redisplays an old cell it actually just dequeues a new one. I think the solution is to use the - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath delegate method:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    id objectForCell = [self.arrayOfThingsForTableView objectAtIndex:indexPath.row];
    if (!objectForCell.button1IsEnabled) { 
        cell.button1.enabled = NO; //or something along those lines
    } else {
        cell.button1.enabled = YES; //necessary so that all the other buttons don't disable
    }
}

If anyone has a better solution, I'd be really happy to hear it.

eman
A: 

Use this:

IGECellBasic *cell = [[[IGECellBasic alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
Ondrej