objective-c - 在自定义单元格中双重选择一个 uitableviewcell

标签 objective-c ios uitableview

我有一个关于 UitableViewCell 的问题。问题是我制作了一个自定义单元格,并且该单元格中有一个复选框图像按钮。我检查并取消选中它。它工作正常,但问题是当我选择第 1 行时。它还会选择第 10 行,与其他行一样,例如 2 它将自动选择第 11 行。我一次显示 10 行。

这是我的 CellForIndexPath 代码

static NSString *cellIdentifier = @"Cell";
InterestsTableViewCell *cell = (InterestsTableViewCell *) [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell==nil) {
    NSArray *arrayNibs = [[NSBundle mainBundle] loadNibNamed:@"InterestsTableViewCell" owner:self options:nil];
    cell = [arrayNibs objectAtIndex:0];
    cell.delegate = self;
    cell.total = [dataArray count];
}

cell.tag = indexPath.row;

cell.lblTitle.text = [dataArray objectAtIndex:indexPath.row];

if (indexPath.row==0) {
    cell.imgBg.image = [UIImage imageNamed:@"tableCell_top_default.png"];
}else if(indexPath.row == [dataArray count]-1){
    cell.imgBg.image = [UIImage imageNamed:@"tableCell_bottom_default.png"];
}else{
    cell.imgBg.image = [UIImage imageNamed:@"tableCell_middle_default.png"];
}

return cell;

另外,我正在检测触摸,以便我可以更改背景图像(我有另一个图像作为背景)。触摸开始的代码(在自定义单元格中如下)

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
//NSLog(@"%i",tag);
isSelected = !isSelected;

btnTickMark.selected = !btnTickMark.selected;
(isSelected ?  (onImage = YES) : (onImage = NO));

有人可以帮助我解决这个问题,为什么当我单击一行时选择了 2 行。

提前致谢

最佳答案

这是因为 UITableView 使单元格出队。当您在每个屏幕上显示 10 行时,它完全适合,第 11 行显示为已选中,因为它已从第 1 行出队。

为避免这种行为,您必须向 tableView:cellForRowAtIndexPath: 添加检查以设置每个单元格的检查参数 - 就像您为每个单元格设置的文本一样。

关于objective-c - 在自定义单元格中双重选择一个 uitableviewcell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7021343/

相关文章:

ios - 提议的 Destination IndexPath 由于 header 错误

ios - MapBox map 整合

ios - 在 iOS 中使用 Google map 时如何使用自定义标注进行注释?

ios - 为什么返回 View 时获取的数据不消失?

ios - 沙盒 : Is possible to simulate a failed or interrupted In App Purchase?

iOS:在 Swift 中动态设置 UITableView 高度

objective-c - iOS从web读取Json并显示到tableView

ios - UIScrollView 中 subview 的 touchEvent

ios - iOS 中的每个应用程序都具有相同的 deviceToken 吗?

ios - 如何让 iOS 在 tableView 上显示删除按钮,但不允许从右侧完全滑动以删除