如何在uitableview中选择一行后停止另一行选择

时间:2014-12-31 08:29:03

标签: uitableview didselectrowatindexpath

在tableview中选择didselectrow中的一行我使用了这个

iPhone :UITableView CellAccessory Checkmark

通过选择一行来调用网络服务

问题:我希望下次只做一行选择而不是另一行做什么 阻止其他行被选中

1 个答案:

答案 0 :(得分:0)

tableView.allowsSelection = false添加到didSelectRowAtIndexPath:,然后在适当的时间将其重新设置为true(我猜测您的网络服务完成后)。

<强> ADDED

要使其无法再次选择所选行,我会添加

夫特:

var selectedRows = [Int]()

作为实例变量(即在类级别,而不是在方法中)。

然后我会将didSelectRowAtIndexPath:更改为:

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    if !(selectedRows as NSArray).containsObject(indexPath.row) {
        // Request data from web service because this row has not been selected before
        selectedRows.append(indexPath.row) // Add indexPath.row to the selectedRows so that next time it is selected your don't request data from web service again
        let cell = tableView.cellForRowAtIndexPath(indexPath) as UITableViewCell
        cell.selectionStyle = UITableViewCellSelectionStyle.None
    }
}

目标-C:

@property (strong, nonatomic) NSMutableArray *selectedRows;

在视图控制器中,初始化selectedRows

- (NSMutableArray *)selectedRows
{
    if (!_selectedRows) {
        _selectedRows = [[NSMutableArray alloc] init];
    }
    return _selectedRows;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (![self.selectedRows containsObject:indexPath.row]) {
        // Request data from web service because this row has not been selected before
        [self.selectedRows addObject:indexPath.row]; // Add indexPath.row to the selectedRows so that next time it is selected your don't request data from web service again
        UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath]
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    }
}