奇怪的行为 - 直到三次点击,行才会重新选择

时间:2014-12-23 03:13:22

标签: ios objective-c uitableview

我目前在iOS中遇到一个UITableView的奇怪问题,因此不再可以立即选择取消选择的行,您必须再点击三次才能再次选择。我正在实施类似于清单的东西,有一个“清除”按钮可以通过并取消选择所有单元格。仅在先前选择了单元格但按下“清除”按钮时才会出现此效果。如果一个单元格之前没有被触及,并且在清除按钮之后被触摸,它将被呈现并且切换正常。

这就是我所拥有的

我应该提到这个表所在的VC存储在容器中,因此引用它。

取消选择

- (IBAction)btnClearTapped:(id)sender {
    UITableView *locationList = self.locationsVc.tableView;

    for (NSUInteger i = 0; i < [locationList numberOfSections]; i++) {
        for (NSUInteger j = 0; j < [locationList numberOfRowsInSection:i]; j++) {
            [locationList deselectRowAtIndexPath:[NSIndexPath indexPathForRow:j inSection:i] animated:YES];
        }
    }
}

didSelectRowAtIndexPath方法

- (void) tableView: (UITableView *) tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
    BOOL isChecked = !((NSNumber *) cellToggleDict[indexPath]).boolValue;
    cellToggleDict[indexPath] = @(isChecked);
    UIView *selectionColorView = [[UIView alloc] init];
    if (isChecked) {
        selectionColorView.backgroundColor = selectedColor;
        selectedLocations[indexPath] = [[Search sharedManager] locationAtIndexPath:indexPath];//PFObject
    }
    else {
        selectionColorView.backgroundColor = unselectedColor;
        [selectedLocations removeObjectForKey:indexPath];
    }
    cell.selectedBackgroundView = selectionColorView;
}

最后是出队和重绘方法

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Reuse" forIndexPath:indexPath];

    PFObject *object = [[Search sharedManager] locationAtIndexPath:indexPath];

    cell.textLabel.text = object[@"Name"];
    BOOL isChecked = ((NSNumber *) cellToggleDict[indexPath]).boolValue;

    UIView *selectionColorView = [[UIView alloc] init];
    if (isChecked) {
        selectionColorView.backgroundColor = selectedColor;
    }
    else {
        selectionColorView.backgroundColor = unselectedColor;
    }

    cell.selectedBackgroundView = selectionColorView;

    return cell;
}

1 个答案:

答案 0 :(得分:1)

您不会清除cellToggleDict方法中的btnClearTapped:条目。你应该有。

- (IBAction)btnClearTapped:(id)sender {
    UITableView *locationList = self.locationsVc.tableView;

    [cellToggleDict removeAllObjects];    // Clear all objects from the selected store

    for (NSUInteger i = 0; i < [locationList numberOfSections]; i++) {
        for (NSUInteger j = 0; j < [locationList numberOfRowsInSection:i]; j++) {
            [locationList deselectRowAtIndexPath:[NSIndexPath indexPathForRow:j inSection:i] animated:YES];
        }
    }
}