开发者

Checkmarks in a UITableView

I create a tableview with 20 cells.

And when I select first row, it will show checkmark on it.

Checkmarks in a UITableView

But when I scroll tableview, then it not only one checkmark on tableview cell.

It also show on another cell.

Checkmarks in a UITableView

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 dequeueReusableCellWithId开发者_StackOverflow中文版entifier: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 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;*


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


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜