0

なぜこれが機能しないのか疑問に思っています。助けてください。これが私のコードです

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

*)indexPath
{
    if (selectedIndexPath == indexPath) {
        selectedIndexPath = nil;
    } else {
        selectedIndexPath = indexPath;
    }
    [self.tableView deselectRowAtIndexPath : indexPath animated : NO];
    [tableView beginUpdates];
    [tableView endUpdates];
}
4

2 に答える 2

0

誤ってselectedIndexPathに設定されないように、必ず を保持してください。nil

@interface YourClass

@property (nonatomic, strong) NSIndexPath *selectedIndexPath;

@end

次に、変数を as として参照self.selectedIndexPathすれば問題ありません。

しかし、2 つ目の注意点があります。NSIndexPathはオブジェクトなので、演算子はポインタ アドレス==を比較するだけです。1 つはユーザーの変数で、もう 1 つはメソッドのプライベート変数であるため、これらは常に異なります。あなたが使いたいのはどちらかです

if ([indexPath isEqualTo:selectedIndexPath]) // not tested

また

if (indexPath.row == selectedIndexPath.row && 
    indexPath.section == selectedIndexPath.section)
于 2012-06-19T05:13:56.687 に答える
0

NSIndexPathオブジェクトは、行とセクションの 2 つの部分で構成されます。このindexPathForRow:inSection:メソッドは、NSIndexPath行とセクションのインデックス番号からオブジェクトを作成します。selectedIndexPathが正しく作成されていることを確認してください。詳細については、このアップルのリファレンスに従ってください。

于 2012-06-19T05:21:14.957 に答える