我有一个UITableView充满了对象。在didSelectRowAtIndexPath方法我有一个UITableViewCellAccessoryCheckmark当行被选中时出现,并在未选中时消失。UITableViewCellAccessory消失滚动关闭屏幕
继承人为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;
}
我的问题是,当所选择的单元格滚动在视图之外,与该值相关联的复选标记现在消失。
我应该怎么做才能使对号消失?
谢谢
如果我依靠用户点击选择行,我如何设置cellForRowAtIndexPath中的复选标记?你能否介绍一下我的例子? – 2011-04-29 02:01:26
当他们点击该行设置复选标记时,需要将该信息存储在模型或控制器本身中。你可以做一些简单的事情,例如当用户点击一行时,将检查的状态存储在字典中的索引路径,然后使用它在cellForRowAtIndexPath中设置检查的状态。 – 2011-04-29 02:09:30