3

UITableView字幕セル用に構成された があります。にセルを追加すると、TableView2 つの配列ができます。1 つはサブタイトル用で、もう 1 つはメイン ラベル用です。またNSMutableDictionary、セルをテーブルに追加すると、オブジェクトとキーが記録されるようにします。UITableView私の問題は、このメソッドを使用してセルを削除したいときです:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
    [myArray removeObjectAtIndex:indexPath.row];
    [numberArray removeObjectAtIndex:indexPath.row];


    [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
} else if (editingStyle == UITableViewCellEditingStyleInsert) {
    // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
}
}

これは、関連付けられた配列からオブジェクトを削除するのに最適ですが、作成したオブジェクトからも削除したいと考えていますMutableDictionary。私が配列で使用したものと同様のものを使用すると思いました。

[myArray removeObjectAtIndex:indexPath.row];

でも使えない

 [myDictionary removeObjectForKey:indexPath.row];

私がこれをどのように書くか知っている人はいますか?

4

2 に答える 2

3

indexPath.rowはプリミティブ型であるため、 のキーとして使用できません。そのためには、 でNSDictionaryラップする必要があります。NSNumber

[myDictionary removeObjectForKey:[NSNumber numberWithInt:indexPath.row]];

NSNumberこれは、ラップされた整数を のキーとして使用する場合にのみ機能しますNSMutableDictionary。一般に、NSDictionaryは連想コンテナであるため、 の呼び出しで使用したのと同じキーを の呼び出しで使用する必要がありremoveObjectForKeyますsetObject:forKey:

于 2012-11-26T01:30:59.960 に答える