0

さて、私はゆっくりとこれを理解しています。もう1つだけ問題があります。私は文字列を使用しており、文字列がセルテキストと等しい場合は、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 に読み込まれたチェックマークが消えません。どうしてこれなの?

4

2 に答える 2

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];

}

プロジェクト全体を表示するためにダウンロードできる新しいサンプルアプリがあります。

于 2012-11-04T01:10:17.040 に答える
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もちろん、 type のプロパティが必要ですNSIndexPath *

このコードでは、0 行または 1 行を選択できます。

于 2012-11-04T01:03:17.623 に答える