我有一个充满对象的 UITableView。在里面didSelectRowAtIndexPath方法我有一个 UITableViewCellAccessoryCheckmark 在选择行时出现并在取消选择时消失。
这是代码didSelectRowAtIndexPath method:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:NO];
UITableViewCell *curCell = [beerTable cellForRowAtIndexPath:indexPath];
if (curCell.accessoryType == UITableViewCellAccessoryCheckmark) {
[curCell setAccessoryType:UITableViewCellAccessoryNone];
compareCount = (compareCount - 1);
if (tableView == [[self searchDisplayController] searchResultsTableView]) {
NSString *objBeer = [searchResults objectAtIndex:indexPath.row];
[compareBeers removeObject:[searchResults objectAtIndex:indexPath.row]];
[compareCarbs removeObject:[carbAmount objectAtIndex:[beerNames indexOfObject:objBeer]]];
}
else {
[compareBeers removeObject:[beerNames objectAtIndex:indexPath.row]];
[compareCarbs removeObject:[carbAmount objectAtIndex:indexPath.row]];
}
}
else {
[curCell setAccessoryType:UITableViewCellAccessoryCheckmark];
compareCount = (compareCount + 1);
if (tableView == [[self searchDisplayController] searchResultsTableView]) {
NSString *objBeer = [searchResults objectAtIndex:indexPath.row];
[compareBeers addObject:[searchResults objectAtIndex:indexPath.row]];
[compareCarbs addObject:[carbAmount objectAtIndex:[beerNames indexOfObject:objBeer]]];
}
else {
[compareBeers addObject:[beerNames objectAtIndex:indexPath.row]];
[compareCarbs addObject:[carbAmount objectAtIndex:indexPath.row]];
}
}
if (compareCount > 0) {
if (compareOn == YES){
}
else {
compareButton.enabled = YES;
UIImage *image = [UIImage imageNamed:@"redbutton.png"];
[compareButton setImage:image];
}
}
else {
compareButton.enabled = NO;
[compareButton setImage:nil];
[compareButton setCustomView:nil];
}
}
我也有这个作为我的索引路径单元格:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"CustomCell";
CustomCell *cell = (CustomCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
for (id currentObject in topLevelObjects){
if ([currentObject isKindOfClass:[UITableViewCell class]]){
cell = (CustomCell *) currentObject;
break;
}
}
}
// Setting separate tables correctly....
return cell;
}
我的问题是,当选定的单元格滚动到视图之外时,与该值关联的复选标记现在在返回视图时消失。
我应该怎样做才能防止复选标记消失?
Thanks
当您滚动数据时,您的单元格会被重新使用(这就是 dequeueReusableCellWithIdentifier 的作用)。在 didSelectRowAtIndexPath 中获得复选标记的单元格将被回收用于不同的行,并且不再与选中的行有任何连接。
您需要在 cellForRowAtIndexPath 中设置/取消设置辅助视图,以便当选中的行滚动回视图时,它们会得到适当的检查。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)