选择行后UITableViewCell附件选中标记不保存在滚动上

时间:2016-10-26 14:35:34

标签: ios objective-c uitableview

我最多只能存储3件允许随时检查的物品。

我将所选行存储在NSMutabeDictionary

中名为selectedRowDictionary的{​​{1}}中

然后在我的didSelectRowAtIndexPath

cellForRowAtIndexPath

- - - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"identifer" forIndexPath:indexPath]; if (_selectedRowDictionary && [_selectedRowDictionary count]) { for (NSString *rowSelected in _selectedRowDictionary) { BOOL isRowSelected = [[_selectedRowDictionary valueForKey:rowSelected] integerValue]; if (isRowSelected){ NSLog(@"rowSelected: %@", rowSelected); } else { NSLog(@"rowNotSelected: %@", rowSelected); } int rowIndexSelected = [[rowSelected substringFromIndex:[rowSelected length] - 1 ] integerValue]; if (isRowSelected && rowIndexSelected == indexPath.row) { cell.accessoryType = UITableViewCellAccessoryCheckmark; } else { cell.accessoryType = UITableViewCellAccessoryNone; } } } return cell; }

didSelectRowAtIndexPath:

当我向下滚动桌面视图时,我是否遗漏了导致单元格再次被取消选择的内容?

当我- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; if (cell.accessoryType == UITableViewCellAccessoryCheckmark) { cell.accessoryType = UITableViewCellAccessoryNone; if (_selectedRowDictionary) { [_selectedRowDictionary removeObjectForKey:[NSString stringWithFormat:@"row%d", indexPath.row]]; NSLog(@"row %d removed from array", indexPath.row); } } else { cell.accessoryType = UITableViewCellAccessoryCheckmark; [_selectedRowDictionary setValue:[NSNumber numberWithBool:YES] forKey:[NSString stringWithFormat:@"row%d", indexPath.row]]; } if ([_selectedRowDictionary count] > 3) { UITableViewCell *lastSelectedCell = [tableView cellForRowAtIndexPath:indexPath]; lastSelectedCell.accessoryType = UITableViewCellAccessoryNone; [_selectedRowDictionary removeObjectForKey:[NSString stringWithFormat:@"row%d", indexPath.row]]; NSLog(@"row selected > 3, row%d not selected", indexPath.row); } } 字典时,它表示这些行存在且已被选中

我已经检查了类似的问题,但我认为我已经解决了细胞回收问题。

2 个答案:

答案 0 :(得分:1)

您是否可以自由使用可变数组?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"identifer" forIndexPath:indexPath];

    if ([_selectedRowArray containsObject:indexPath])
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    else
        cell.accessoryType = UITableViewCellAccessoryNone;

    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    if ([_selectedRowArray containsObject:indexPath]) {
        [_selectedRowArray removeObject:indexPath];
    } else {
        if (_selectedRowArray.count < 3)
            [_selectedRowArray addObject:indexPath];
        else {
            // Don't select it
        }
    }

    [tableView reloadData]; 
}

答案 1 :(得分:1)

我正在编辑您的代码,请尝试此代码。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"identifer" forIndexPath:indexPath];
   BOOL isRowSelected = [[_selectedRowDictionary valueForKey:@(indexPath.row)] boolValue]
   if (isRowSelected) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    } else {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }  
   return cell;
}

代码更改 - didSelectRowAtIndexPath

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];


if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {
    cell.accessoryType = UITableViewCellAccessoryNone;

    if (_selectedRowDictionary) {
        [_selectedRowDictionary removeObjectForKey:@(indexPath.row)];
        NSLog(@"row %d removed from array", indexPath.row);
    }

} else {
    if (_selectedRowDictionary.count == 3) {
       // Don't allow for Selection
       return;
    }
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
    [_selectedRowDictionary setValue:@(YES) forKey:@(indexPath.row)];
   }
}

用户必须取消选择上一个选定的行才能选择新行。我以优化的方式编辑了您的代码,希望这段代码适合您。