ios - UITableView选择——多选

标签 ios objective-c uitableview

我有一个奇怪的 uitableview 选择问题。尝试对预选的几个单元格使用多重选择。多选设置为真。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
    cell.textLabel.font = [UIFont systemFontOfSize: 15.0f];
    cell.textLabel.text = [data objectAtIndex:indexPath.row];

    //    if([_selectedInts containsObject:[NSNumber numberWithInt:(int)indexPath.row]]){
    //
    //        [cell setSelected:YES animated:NO];
    //
    //    } else {
    //
    //        [cell setSelected:NO animated:NO];
    //
    //    }


    return cell;
}



-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{

    cell.selectionStyle = UITableViewCellSelectionStyleGray;
    [cell setUserInteractionEnabled:YES];


    if([_selectedInts containsObject:[NSNumber numberWithInt:(int)indexPath.row]]){

        [cell setSelected:YES animated:YES];
        [cell setAccessoryType:UITableViewCellAccessoryCheckmark];
    } else {

        [cell setSelected:NO animated:YES];
        [cell setAccessoryType:UITableViewCellAccessoryNone];
    }

}

当 tableview 加载时,由于整数列表标记了需要选择的索引,因此选择了一堆单元格。

这些单元格停止响应,并且在被点击时不会发出 didselect 和 diddeselect 方法。

问题是什么?

最佳答案

我不确定为什么将该代码放入 willDisplayCell 中会导致这种情况发生,但当我尝试您的代码时,我发现了同样的事情。将代码放入 cellForRowAtIndexPath: 如果我使用 selectRowAtIndexPath:animated:scrollPosition: 而不是 setSelected: 则有效(如果我使用 setSelected:,单元格不会显示选择颜色,但它们会响应)。因此,此代码可以显示复选标记和选择颜色,并在选择或取消选择其他单元格时正确更改该状态。另请注意,我将单元出队,因为这比您创建单元的方式内存效率更高,

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    cell.textLabel.font = [UIFont systemFontOfSize: 15.0f];
    cell.textLabel.text = [data objectAtIndex:indexPath.row];

    if([_selectedInts containsObject:@(indexPath.row)]){
        [tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
        [cell setAccessoryType:UITableViewCellAccessoryCheckmark];
    } else {
        [cell setAccessoryType:UITableViewCellAccessoryNone];
    }
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [self.selectedInts addObject:@(indexPath.row)];
    [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
}


-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self.selectedInts removeObject:@(indexPath.row)];
    [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
}

关于ios - UITableView选择——多选,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23181250/

相关文章:

iphone - 使用 iPhone 中的自定义按钮执行 MFMessageComposeViewController 中的按钮操作

ios - 在管理器 loadFromPreferencesWithCompletionHandler 上获取权限被拒绝错误

iOS,UITabBarController 中的问题

swift - 使用swift 4在表格 View 中选择行时显示两个按钮

ios - 从 JSON 数据将 null 分配给 NSString 时崩溃

ios - Swift-Xcode : why isn't my self. tableView.reloadData() 调用将 promise 的数据加载到我的表/单元格中?

ios - 为什么 Interface Builder 通过其上方的 View 显示隐藏的 View ?

ios - Sprite 未从场景 iOS 中移除

ios - 在 Objective-C 中访问 Swift OptionSetType

javascript - 如何识别 FlipBoard 或 MobileRss 中的自定义 UIWebView