0

ロード後に最初のセルを確認すると、何も起こりません。何度も何度もタップしていますが、何も起こりません。他のセル、2 番目、3 番目などを確認でき、その後で初めて最初のセルを確認できます。これは私の方法です:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSUInteger row = indexPath.row;
    NSUInteger oldRow = lastIndexPath.row;
    if (oldRow != row) {
        UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath]; 
        newCell.accessoryType = UITableViewCellAccessoryCheckmark;
        UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:lastIndexPath];
        oldCell.accessoryType = UITableViewCellAccessoryNone;
        lastIndexPath = indexPath;
    }
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

または、コードが多くて理解しにくいモデルだけを見つけたので、他の方法でそれを作成するようにアドバイスすることもできます (テーブルビューで 1 つのセルのみをチェックする)。

4

2 に答える 2

2

これは、最初はlastIndexPath変数がnilであるため、lastIndexPath.row0 が返されるためです。最初の行をタップすると、その行も 0 であるため、ifステートメントには入りません。そのステートメントを次のように置き換えます。if (!lastIndexPath || oldRow != row)

于 2013-11-12T13:02:36.850 に答える
0
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell* cell;
    //cell creation code
    cell.accessoryType = nil != lastIndexPath && lastIndexPath.row == indexPath.row ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSArray* reloadRows = nil == lastIndexPath ? @[indexPath] : @[lastIndexPath, indexPath];
    lastIndexPath = indexPath;
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    [tableView reloadRowsAtIndexPaths:reloadRows withRowAnimation: UITableViewRowAnimationAutomatic];
}
于 2013-11-12T13:15:24.560 に答える