UITableView 中的复选标记

发布于 2024-11-08 18:12:12 字数 2042 浏览 0 评论 0原文

我创建了一个包含 20 个单元格的表格视图。
当我选择第一行时,它会显示复选标记。

在此处输入图像描述

但是当我滚动表格视图时,它不仅仅是表格视图单元格上的一个复选标记。
它也显示在另一个单元格上。

在此处输入图像描述

这有什么问题?

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"];
    }
}

I create a tableview with 20 cells.
And when I select first row, it will show checkmark on it.

enter image description here

But when I scroll tableview, then it not only one checkmark on tableview cell.
It also show on another cell.

enter image description here

What's the problem with this?

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"];
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

指尖凝香 2024-11-15 18:12:12

我认为这是由于重复使用了复选标记的单元格。要纠正这个问题,请写下:

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

单元格重用空间不足。那是之后:
...
}
//配置单元格。

就在之前

*return cell;*

I think it is due to reusing the checkmarked cell. To correct that, write this:

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

Out of cell reusing space. That is After:
...
}
//Configure the cell.

just before

*return cell;*
丶视觉 2024-11-15 18:12:12

试试这个。这里selectedRow是一个整数。在viewDidLoad中指定selectedRow = -1。

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

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {

    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];

}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
int row = [indexPath row];
cell.accessoryType = (row == selectedRow) ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

selectedRow = [indexPath row];

[tableView reloadData];
}

Try this. Here selectedRow is an integer .Assign selectedRow = -1 in viewDidLoad.

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

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {

    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];

}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
int row = [indexPath row];
cell.accessoryType = (row == selectedRow) ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

selectedRow = [indexPath row];

[tableView reloadData];
}
洒一地阳光 2024-11-15 18:12:12

检查这个

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *thisCell = [tableView cellForRowAtIndexPath:indexPath];


    if (thisCell.accessoryType == UITableViewCellAccessoryNone) {
        thisCell.accessoryType = UITableViewCellAccessoryCheckmark;

    }else{
        thisCell.accessoryType = UITableViewCellAccessoryNone;

    }
    }

    - (UITableViewCellAccessoryType)tableView:(UITableView *)tableView accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath {

    //add your own code to set the cell accesory type.
    return UITableViewCellAccessoryNone;
    }

check this

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *thisCell = [tableView cellForRowAtIndexPath:indexPath];


    if (thisCell.accessoryType == UITableViewCellAccessoryNone) {
        thisCell.accessoryType = UITableViewCellAccessoryCheckmark;

    }else{
        thisCell.accessoryType = UITableViewCellAccessoryNone;

    }
    }

    - (UITableViewCellAccessoryType)tableView:(UITableView *)tableView accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath {

    //add your own code to set the cell accesory type.
    return UITableViewCellAccessoryNone;
    }
只怪假的太真实 2024-11-15 18:12:12

我遇到了同样的问题。对我来说,解决方案是添加一行代码。

cell.accessoryType = UITableViewCellAccessoryNone;

添加上面的代码:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }

此解决方案对于发布问题的编码人员可能没有用,但可能会帮助像我这样面临与原始问题提出的相同问题的新人。

i faced same issue. for me, solution was adding a single line of code.

cell.accessoryType = UITableViewCellAccessoryNone;

adding the above code after:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }

this solution may not be useful for coder who posted the question, but may help new folks like me who face same issue as raised by the original quesiton.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文