滚动屏幕时UITableViewCellAccessory消失 [英] UITableViewCellAccessory Disappears When Scrolled Off Screen
问题描述
我有一个充满对象的UITableView。在 didSelectRowAtIndexPath 方法中,我选择了行时显示UITableViewCellAccessoryCheckmark,取消选择时消失。
I have a UITableView filled with objects. In the didSelectRowAtIndexPath method i have a UITableViewCellAccessoryCheckmark appear when the row is selected and disappear when unselected.
继承 didSelectRowAtIndexPath的代码方法:
- (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];
}
}
我也有这个作为我的 cellForIndexPath :
- (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;
}
我的问题是当选中的单元格滚出视图时与该值相关联的复选标记现在在返回视图时消失。
My problem is that when the cell that is selected is scrolled out of view the checkmark associated with that value is now gone when back into view.
我应该怎样做才能使Checkmark不再消失?
What should I do to keep the Checkmark from disappearing?
谢谢
推荐答案
滚动浏览数据时,您的单元格会被重复使用(这就是dequeueReusableCellWithIdentifier所做的)。在didSelectRowAtIndexPath中获得复选标记的单元格将被回收用于不同的行,并且不再与已检查的行有任何连接。
Your cells get re-used as you scroll through the data (that's what the dequeueReusableCellWithIdentifier does). Your cell that got a checkmark in didSelectRowAtIndexPath gets recycled for a different row and no longer has any connection to the checked row.
您需要设置/取消设置附件视图cellForRowAtIndexPath所以当被检查的行回滚到视图中时,它们会被适当地检查。
You need to set/unset the accessory view in cellForRowAtIndexPath so when the checked rows scroll back into view, they get checked appropriately.
这篇关于滚动屏幕时UITableViewCellAccessory消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!