UITableView中的选中标记

min*_*ieh 6 iphone xcode uitableview

我创建了一个包含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"];
    }
}
Run Code Online (Sandbox Code Playgroud)

小智 6

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

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

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

就在此之前

*return cell;*
Run Code Online (Sandbox Code Playgroud)


小智 5

试试这个.这里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];
}
Run Code Online (Sandbox Code Playgroud)


Nar*_*thy 2

检查这个

- (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;
    }
Run Code Online (Sandbox Code Playgroud)