【发布时间】:2012-03-23 17:05:57
【问题描述】:
我正在尝试制作一个 UITableView,它向选定的单元格添加复选标记,发生的情况是它确实添加了复选标记但不是立即添加,当我选择不同的行时它会显示它。知道如何使它成为瞬时的吗?这是我的代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Contact";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSLog(@"%@", [[self.contacts objectAtIndex:indexPath.row] valueForKeyPath:@"TwitterFriend.screen_name"]);
cell.textLabel.text = [[self.contacts objectAtIndex:indexPath.row] valueForKeyPath:@"TwitterFriend.screen_name"];
if ([self.selectedContacts containsObject:[self.contacts objectAtIndex:indexPath.row]]) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
[self.selectedContacts addObject:[self.contacts objectAtIndex:indexPath.row]];
[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
}
【问题讨论】:
-
您可以尝试将该逻辑放入
tableView:didSelectRowAtIndexPath而不是tableView:didDeselectRowAtIndexPath。 -
什么?您知道您两次粘贴相同的方法吗? :P
-
天哪,我刚刚注意到我使用了错误的方法,这就是我信任自动完成的结果。多谢,伙计。你能发布一个答案让我接受吗?
标签: ios uitableview