【问题标题】:iOS - UITableViewCell not displaying check mark after being selectediOS - UITableViewCell 被选中后不显示复选标记
【发布时间】: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


【解决方案1】:

您似乎在tableView:didDeselectRowAtIndexPath 中执行您的逻辑,但默认情况下,在单元格选择更改之前不会调用此方法。如果您改为将其放在tableView:didSelectRowAtIndexPath 中(注意名称略有不同),您将在点击单元格后立即收到回调,并且附件视图应立即出现。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-23
    • 1970-01-01
    • 1970-01-01
    • 2014-10-17
    • 2012-03-16
    相关资源
    最近更新 更多