TableViewController选择性单元格在表重新布局时取消选择

时间:2018-11-21 06:22:59

标签: ios objective-c

我有一个问题,当我尝试使用刷新控制器重新加载行/表并立即滚动tableview时,某些单元格被取消选择。我在diSelectrow和didDeselectRow中用取消选择检查了它。在两种方法中都没有遇到调试计数器。 我在cellforRow中有以下代码

@IBAction func onBtnClicked(_ sender: Any) { 
        let value = "My value to be encrypted"
        let key = "MySixteenCharKey"

        print(key!)
        let encryptedValue = try! value.aesEncrypt(key: key!)

        print(encryptedValue)

    }

遵循didselectrow

if (isEditClicked == true || [indexarray containsObject:indexPath]) {
    [cardinboxCardCell setSelectionStyle:UITableViewCellSelectionStyleDefault];
}

遵循didDeselctrow

if (!self.tableView.isEditing) {
    Card *card;
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    card = [_fetchedResultsController objectAtIndexPath:indexPath];
    [self performSegueWithIdentifier:@"pushToMyCardDetails" sender:card];
}else {
    selectedDeleteCardCount += 1;
    if (deleBtn) {
        [deleBtn setEnabled:true];
    }
    [indexarray addObject:indexPath];
    NSString* cardsCount = [NSString stringWithFormat:@"(%d)",selectedDeleteCardCount];
    [self.navigationController.navigationBar.topItem setTitle:cardsCount];
}

任何线索都会有所帮助。谢谢。

4 个答案:

答案 0 :(得分:0)

我对indexpath对象有疑问,可能是该对象已更改..这就是为什么这种情况失败的原因

if (isEditClicked == true || [indexarray containsObject:indexPath]) {

我对您的建议是检查节和行的索引,而不是像这样的对象本身

//find indexPath (row and section) in indexarray using for loop to retrive whether 
//is this cell is selected or not

     bool isCellSelected = [self currentIndexPathSelected:indexPath];
    if (isEditClicked == true || isCellSelected) {
    }

答案 1 :(得分:0)

您只需要维护所有单元的状态。您可以使用Bool isSelected。每当选择任何单元格时,在状态索引处将其标记为true。与取消选择标记为false相同。 :-)

答案 2 :(得分:0)

请勿尝试在用户界面中保持选定状态。独立存储它,以便您在重新加载表格视图时可以将其还原。

我一般建议不要存储选定的索引或indexPath,因为如果插入/删除/重新排序项目,则它们与模型之间的关系可能会改变。 (虽然可能不适用于您的情况,但最好还是避免此潜在问题)

相反,存储选定的模型对象,则您将始终确切地知道选择了什么,并且什么都不会搞乱。只需将可变数组设为“选定”,然后根据需要添加即可删除它们。

didSelectRowAtIndexPath是您从所选数组中添加/删除模型对象的地方。 cellForRowAtIndexPath是您根据所选数组设置单元格所选状态的地方。

答案 3 :(得分:-1)

为了实现您想要的功能,您的设置应如下所示:

enter image description here

现在专注于这一部分:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    NSDictionary *object = _objects[indexPath.row];
    cell.textLabel.text = object[@"text"];
    cell.imageView.image = [UIImage imageNamed:object[@"image"]];
    if (_selectedIndexPaths[[self getKey:indexPath]] != nil) cell.selected = [_selectedIndexPaths[indexPath] boolValue];
    return cell;
}

- (NSString *) getKey : (NSIndexPath *) path {
    return [NSString stringWithFormat:@"%ld-%ld", (long)path.section, (long)path.row];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [_selectedIndexPaths setValue:@(TRUE) forKey:[self getKey:indexPath]];
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    [cell setSelected:true];

    //[tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}

如您所见,在选择单元格时,我们在_selectedIndexPaths中设置了一个值,并且在构造单元格时,该检查使我们能够保持选择状态。您也可以将相同的图案与checkMark附件一起使用。