Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checkmarks in a UITableView

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"];
    }
}
like image 583
miniHsieh Avatar asked May 19 '11 11:05

miniHsieh


People also ask

How can we use a reusable cell in Uitableview?

For performance reasons, a table view's data source should generally reuse UITableViewCell objects when it assigns cells to rows in its tableView(_:cellForRowAt:) method. A table view maintains a queue or list of UITableViewCell objects that the data source has marked for reuse.

How does a tableView work?

A table view displays a single column of vertically scrolling content, divided into rows and sections. Each row of a table displays a single piece of information related to your app. Sections let you group related rows together. For example, the Contacts app uses a table to display the names of the user's contacts.

What is IndexPath Uitableview?

IndexPath contains information about which row in which section the function is asking about. Base on this numbers you are configuring the cell to display the data for given row.


1 Answers

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;*
like image 91
LuisRdF Avatar answered Sep 21 '22 22:09

LuisRdF