iOS   发布时间:2022-05-04  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了iphone – UITableView中的选中标记大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

我创建了一个包含20个单元格的tableview. 当我选择第一行时,它会在上面显示复选标记. 但是当我滚动tableview时,它不仅在tableview单元格上有一个复选标记. 它也显示在另一个细胞上. 这有什么问题? self.dataAry = [NSArray arrayWithObjects:@"0",@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"
我创建了一个包含20个单元格的tableview.
当我选择第一行时,它会在上面显示复选标记.

但是当我滚动tableview时,它不仅在tableview单元格上有一个复选标记.
它也显示在另一个细胞上.

这有什么问题?

self.dataAry = [NSArray arrayWithObjects:@"0",@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10",@"11",@"12",@"13",@"14",@"15",@"16",@"17",@"18",@"19",nil];
marks = [NSMutableArray new];
for (int i = 0 ; i < [self.dataAry count]; i++) {
    [marks addObject:@"NO"];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowATindexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reusEIDentifier:CellIdentifier] autorelease];
}
cell.textLabel.text = [dataAry objectATindex:indexPath.row];
if ([[marks objectATindex:indexPath.row] isEqualToString:@"YES"]) {
    [cell setAccessoryType:UITableViewCellAccessorycheckmark];
}else {
    [cell setAccessoryType:UITableViewCellAccessoryNone];
}
// Configure the cell.

return cell;}
- (void)tableView:(UITableView *)tableView didSELEctRowATindexPath:(NSIndexPath *)indexPath{

UITableViewCell *thisCell = [tableView cellForRowATindexPath:indexPath];


if ([[tableView cellForRowATindexPath:indexPath] accessoryType] == UITableViewCellAccessorycheckmark){
        [[tableView cellForRowATindexPath:indexPath] setAccessoryType:UITableViewCellAccessoryNone];
        [SELEctArray removeObject:[self.dataAry objectATindex:indexPath.row]];
        [marks replaceObjectATindex:indexPath.row withObject:@"NO"];
    }
    else {
        [[tableView cellForRowATindexPath:indexPath] setAccessoryType:UITableViewCellAccessorycheckmark];
        [SELEctArray addObject:[self.dataAry objectATindex:indexPath.row]];
        [marks replaceObjectATindex:indexPath.row withObject:@"YES"];
    }
}

解决方法

我认为这是由于重新使用经过检查标记的单元格.要纠正这个问题,请写下:

*if ([[marks objectATindex:indexPath.row] isEqualToString:@"YES"]) {
[cell setAccessoryType:UITableViewCellAccessorycheckmark];
}else {
[cell setAccessoryType:UITableViewCellAccessoryNone];*

脱离细胞重用空间.那是后:
 …
    }
    //配置单元格.

在此之前

*return cell;*

大佬总结

以上是大佬教程为你收集整理的iphone – UITableView中的选中标记全部内容,希望文章能够帮你解决iphone – UITableView中的选中标记所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。