开发者

Issue with dequeueReusableCellWithIdentifier, custom UITableViewCell

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 bu开发者_JAVA技巧tton 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


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.


Use this:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜