我有一个tableview,其中有很多条目分为多个部分,里面有多行。我在每行中都有一个按钮模型化复选框。按下时可以启用/禁用复选框(按钮)。我正在加载一个图像的按钮(复选框)根据状态,无论是否检查。一切都很顺利,直到我试图向下拖动我的UITableview,然后往上走,才发现我的一些选中的复选框会自动取消选中。我有点认为,这是由于使用当uitableview向上和向下滚动时,复选框状态发生变化
dequeuereusablecellwithidentifier
我想避免我的tableview这种奇怪的行为。在这种情况下需要帮助。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
UIButton *checkBox = [[UIButton alloc] init];
checkBox.tag = contact.contactID;
[cell.contentView addSubview:checkBox];
[checkBox setFrame:CGRectMake(6,14,20,20)];
[checkBox release];
}
UIButton *checkBox = (UIButton *)[cell.contentView viewWithTag:contact.contactID];
if(isActivDeactivButton)
{
[checkBox setImage:[UIImage imageNamed:@"disabled_checkbox.png"] forState:UIControlStateNormal];
}
else{
[checkBox setImage:[UIImage imageNamed:@"selected_checkbox.png"] forState:UIControlStateNormal];
}
return cell;
}
我已将我的代码添加到问题中。 – 2012-01-18 18:46:51
这就是你的全部代码吗?我没有看到'contact','isActivDeactivButton'从哪里来。 – Tim 2012-01-18 19:00:42
在您的代码中添加一个NSLog(@“在第%d行的载入单元格”,indexPath.row);然后在滚动时观察输出 - 这将帮助您更好地了解它何时调用方法并会帮助你找出为什么它没有正确设置。 – Tim 2012-01-18 19:01:28