Check box item for UITableView
I am trying to check items of a uitableview but I am having two difficulties: 1. I cannot select the first item 2. if I select the 2nd and 3rd item for instance, this will also place a check mark near the 5th and 7th, and 9th and 10th etc etc
The following is my code:
#pragma mark -
#pragma mark Table View Data Source Methods
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section {
return [self.listArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CustomCellIdentifier = @"CustomCell开发者_StackOverflow社区Identifier";
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CustomCellIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
for (id oneObject in nib)
if ([oneObject isKindOfClass:[CustomCell class]]) {
cell = (CustomCell *)oneObject;
}
}
NSUInteger row = [indexPath row];
if ([selectedArray containsObject:cell])
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
[cell reloadInputViews];
return cell;
}
- (NSIndexPath *)tableView:(UITableView *)tableView
willSelectRowAtIndexPath:(NSIndexPath *) indexPath{
NSUInteger row = [indexPath row];
if (row == 0)
return nil;
return indexPath;
}
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger row = [indexPath row];
UITableViewCell *c = [tableView cellForRowAtIndexPath:indexPath];
if ([selectedArray containsObject:c])
{
c.accessoryType = UITableViewCellAccessoryNone;
[selectedArray removeObject:c];
[selectedIndexes removeObject:[NSNumber numberWithInt:row]];
}
else {
c.accessoryType = UITableViewCellAccessoryCheckmark;
[selectedArray addObject:c];
[selectedIndexes addObject:[NSNumber numberWithInt:row]];
}
}
- (CGFloat) tableView:(UITableView *)tableView
heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return kTableViewRowHeight;
}
- (void)refreshDisplay:(UITableView *)tableView {
[tableView reloadData];
}
I tried using
NSString *identifier = [NSString stringWithFormat:@"Cell %d", indexPath.row];
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:identifier];
instead but that made the check mark disappear when scrolling
Your logic assumes that each row has its own unique cell object. However, the dequeueReusableCellWithIdentifier
stuff means you reuse the cell objects (i.e. when a cell scrolls off the top of the screen, it's reused at the bottom of the screen). You need to redo your logic to not make this assumption.
Also, if (row == 0) return nil;
in willSelectRowAtIndexPath
is what's making you unable to select the first row. Get rid of this line.
You can't select the first item because of this:
- (NSIndexPath *)tableView:(UITableView *)tableView
willSelectRowAtIndexPath:(NSIndexPath *) indexPath{
NSUInteger row = [indexPath row];
if (row == 0)
return nil;
return indexPath;
}
for the rest, don't store cells, store indexpaths. Cells can be reused, and this will cause problems sooner or later. Maybe this is causing the problems you have right now. And save those indexpaths in an NSMutableSet
精彩评论