UITableViewCellAccessoryCheckMark为什么不去?

时间:2012-11-04 00:58:48

标签: objective-c ios xcode uitableview

好的,所以我慢慢搞清楚这一点。我还有一个问题。我正在使用一个字符串,并说如果字符串等于单元格文本,则在加载tableView时对其进行复选标记。

这是我的代码:

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

static NSString *CellIdentifier = @"Cell";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if ([cell.textLabel.text isEqualToString:transferData]) {

    cell.accessoryType = UITableViewCellAccessoryCheckmark;

}

我告诉它删除该复选标记并在选择时相应地添加复选标记:

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

cell.accessoryType = UITableViewCellAccessoryNone;

  UITableViewCell      *cellCheck = [tableView
                              cellForRowAtIndexPath:indexPath];

cellCheck.accessoryType = UITableViewCellAccessoryCheckmark;

  transferData = cellCheck.textLabel.text;
  NSLog(@"%@", transferData);
}


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

UITableViewCell* uncheckCell = [tableView
                                cellForRowAtIndexPath:indexPath];
uncheckCell.accessoryType = UITableViewCellAccessoryNone;

}

除非首次加载,否则一切正常。出于某种原因,当我选择另一个单元格时,最初使用tableView加载的复选标记不会消失。这是为什么?

2 个答案:

答案 0 :(得分:3)

你犯了一个常见的错误。

选择单元格时,您将直接设置复选标记的状态。您应该做的是设置数据源中的复选标记的状态,并让表格单元从数据源配置自己。

已编辑的独家检查表视图示例

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSArray *changedIndexPaths = nil;

    NSIndexPath *currentCheckedIndexPath = [self indexPathOfCurrentCheckedObject];

    if (currentCheckedIndexPath && ![currentCheckedIndexPath isEqual:indexPath]) {
        // There is currently a checked index path - unselect the data source and
        // add it to the changed index array.

        [[self.tableData objectAtIndex:currentCheckedIndexPath.row] setChecked:NO];
        changedIndexPaths = @[indexPath, currentCheckedIndexPath];
    } else{
        changedIndexPaths = @[indexPath];
    }

    [[self.tableData objectAtIndex:indexPath.row] setChecked:YES];

    [self.tableView reloadRowsAtIndexPaths:changedIndexPaths withRowAnimation:UITableViewRowAnimationNone];

}

我有一个新的sample app,您可以下载以查看整个项目:

答案 1 :(得分:2)

你需要:

if (self.selectedPath && [indexPath isEqual:self.selectedPath]) {
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
    cell.accessoryType = UITableViewCellAccessoryNone;
}

细胞被重复使用。如果您有条件地设置任何单元格属性,则必须始终使用“else”部分来重置属性。

修改:在cellForRowAtIndexPath:方法中进行上述更改后,请在didSelectRowAtIndexPath:方法中执行以下操作:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSIndexPath *oldSelection = self.selectedPath;
    if (self.selectedPath) {
        UITableViewCell* uncheckCell = [tableView cellForRowAtIndexPath:self.selectedPath];
        uncheckCell.accessoryType = UITableViewCellAccessoryNone;
        self.selectedPath = nil;
    }

    if (oldSelection == nil || ![indexPath isEqual:oldSelection]) {
        UITableViewCell* checkCell = [tableView cellForRowAtIndexPath:indexPath];
        checkCell.accessoryType = UITableViewCellAccessoryCheckmark;
        self.selectedPath = indexPath;
    }

    [tableView deselectRowAtIndexPath:indexPath];
}

摆脱didDeselectRowAtIndexPath:方法。

当然,您需要selectedPath类型的NSIndexPath *属性。

此代码可让您选择0行或1行。

相关问题