自定义UITableViewCell的dequeueReusableCellWithIdentifier问题 [英] Issue with dequeueReusableCellWithIdentifier, custom UITableViewCell
问题描述
我有2个按钮的自定义单元格(这些按钮的功能只是禁用被按下的按钮). 当我以这种经典方式使用dequeueReusableCellWithIdentifier时:
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;
}
UITableView有1个部分,问题是:在我按下按钮以禁用它的第一个单元格上,而不是向下滚动以显示其他单元格,当我再次向上滚动时,第一个单元格是一个新单元格,而按钮是已启用. 我知道如果已经创建好了reuseIdentifier,它不会重新创建一个单元格,但是这样一来,我丢失了所有不可见的单元格的所有信息.
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.
有什么主意吗?
预先感谢
推荐答案
我遇到了类似的问题-我认为问题在于,在任何给定时间,只有可见单元格才真正在内存中,并且在重新显示旧单元格时单元格实际上只是使一个新队列出队.我认为解决方案是使用- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
委托方法:
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.
这篇关于自定义UITableViewCell的dequeueReusableCellWithIdentifier问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!