自定义UITableViewCell快速滚动的怪异行为

时间:2014-10-22 09:50:12

标签: ios uitableview

我有一个包含5个部分的分组tableView。 tableView使用带有2个标签和4个按钮的自定义UITableViewCell。当我在表的开头选择一个或多个按钮然后滚动到它的结尾时,我发现在最后一个单元格中选择了那些按钮,有时在倒数第二个中。在我看来,dequeueReusableCellWithIdentifier存在一些问题,但我无法弄明白。 为清楚起见,我在viewDidLoad中有这段代码:

// table cell
UINib *nib = [UINib nibWithNibName:@"SMRateMeetingTableViewCell" bundle:nil];
[self.tableView registerNib:nib forCellReuseIdentifier:@"SMRateMeetingTableViewCell"];

这在我的cellForRowAtIndexPath:

SMRateMeetingTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SMRateMeetingTableViewCell" forIndexPath:indexPath];
// Configure the cell...
if (cell == nil) {
    cell = [[SMRateMeetingTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"SMRateMeetingTableViewCell"];
}

非常基本的东西。 我添加了一些屏幕以便更好地理解。 enter image description here

enter image description here

enter image description here

编辑:添加按钮代码。 为了便于分析,假设我在自定义单元格中只有1个按钮 这是表视图代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
SMRateMeetingTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SMRateMeetingTableViewCell" forIndexPath:indexPath];
// Configure the cell...
if (cell == nil) {
    cell = [[SMRateMeetingTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"SMRateMeetingTableViewCell"];
}
//the tag will allow me to understand in which section is the button
cell.firstYesButton.tag = indexPath.section;

[cell.firstYesButton addTarget:self action:@selector(firstYesButtonAction:) forControlEvents:UIControlEventTouchUpInside];

return cell;
}

与按钮相关的相关方法:

-(IBAction)firstYesButtonAction:(id)sender
{
UIButton *senderButton = (UIButton *)sender;
[self.votesArray replaceObjectAtIndex:(senderButton.tag*2) withObject:[NSNumber numberWithInt:1]];
//NSLog(@"%@", self.votesArray );
}

这是自定义单元格的实现文件中的代码:

@implementation SMRateMeetingTableViewCell

- (void)awakeFromNib {
// Initialization code
[self.firstYesButton setImage:[UIImage imageNamed:@"yesRateDisable.png"]   forState:UIControlStateNormal];
}

- (IBAction)firstYesAction:(id)sender {
[self.firstYesButton setImage:[UIImage imageNamed:@"yesRateEnable.png"] forState:UIControlStateNormal];
}

2 个答案:

答案 0 :(得分:1)

这是因为表格视图正在回收您的单元格。这就是dequeueReusableCellWithIdentifier:方法正在做的事情。因为它是循环视图而不是始终创建的新视图,所以您必须设置某些属性,例如按钮的选定状态,否则它们将保留它们排队时所具有的属性。

答案 1 :(得分:0)

由于UITableViewCell被回收,这是非常难以预测的。
因此,一种方法是使用键值对来维持状态
这个NSDictionary和根据状态设置的图像在NSDictionary中改变了

@{
"0":"EnableImage",
"1":"DisableImage",
"2":"DisableImage",
"3":"EnableImage",
}

所以根据cellForRowAtIndexPath中的上述字典设置图像。

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    .... ....
    You need set image as per state maintained in above dictionary

    return cell;
    }

删除以下代码

(void)awakeFromNib {
// Initialization code
[self.firstYesButton setImage:[UIImage imageNamed:@"yesRateDisable.png"]   forState:UIControlStateNormal];
}

- (IBAction)firstYesAction:(id)sender {
[self.firstYesButton setImage:[UIImage imageNamed:@"yesRateEnable.png"] forState:UIControlStateNormal];
}