2016-10-12 2 views
0

私は特定のkeyvalueのNSMutableDictionaryを持っています。この辞書はNSMutableArray内にあります。私は配列内の特定のインデックスにある特定のキーで辞書を更新したいです。この辞書オブジェクトのインデックスを探したい。現在、私の辞書はインデックス0にあり、私のeditingIndexPathは1で、これはNSIndexPathタイプのものなので、editingIndexPath.rowは私を助けません。次のようにNSMutableArray内のNSMutableDictionaryのインデックスの検索

私のコードは次のとおりです。

//UpdatedDateAndTime is the NSMutableArray. 
    //selectedDay is a NSString object. 
    //dateKeySelected is also a string key. 

    [[updatedDateAndTime objectAtIndex:editingIndexPath.row] setObject:selectedDay forKey:dateKeySelected]; 

私の問題は、私が発見された辞書の右のインデックスを取得したいということです。

+0

上記のコードの問題点は何ですか?キーを設定していますか?辞書のインデックスがeditingIndexPath.rowと同じではありませんか? –

+0

@TejaNandamuri ...いいえ!ディクショナリが配列内にすでに存在していた場合はどうなりますか?私にとっては、値が「0」の 'index'にあったのに対して、tableviewでは、この辞書に対応するセルは' editingIndexPath.row'で '1'です。辞書に対応する配列インデックスが必要です。 – Ron

+0

これはインデックス1にあることをどのように知っていますか?どのように辞書を比較するのですか? – Larme

答えて

1

カウンターでループのためにするために使用答えは、しかし、あなたは運がいい:NSArrayのはうまくトリックを行う必要があり、新たな方法、indexOfObject:を、持っている:

NSUInteger i = [myArray indexOfObject:@{myKey:myValue}]; 

スウィフト:index(of:)

+0

私はこの長いインデックス値を取得します。 '9223372036854775807' – Ron

+0

ありがとう!私は正しい指数を得ることができました。なぜ私は正しい辞書オブジェクトを渡していなかったのでこのインデックスを取得していたのですか。 – Ron

0

あなたの配列に特殊なNSMutableDictionaryが1つしか含まれていない場合は、以下のコードを使用してください。私はそれをテストしませんでしたが、考え方は配列のNSMutableDictionaryクラスを検索することです。そして、あなたはいくつかのデータを割り当てるセルに等しいindexPathでこの検索をしなければなりません。

@interface ViewController(){ 
    NSMutableArray *array; 
    NSMutableDictionary *dictionary; 
} 

@end 

@implementation ViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 

} 

- (UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"]; 
    if (indexPath.row == 1) { 
     for (int i = 0; i < array.count; i++) { 
      if ([updatedDateAndTime[i] objectForKey:@"open_house_updated_date"] == selectedDay) { 
      NSLog(@"%i th index contains NSMutableDictionary that you want",i); 
      //do whatever needed 
      break; 
     } 
     } 
    } 
    else{ 
     //define other cells which doesn't contain NSMutableDictionary 
    } 
    return cell; 
} 

@end 

私はこれがあなたが探していることを望みます。

+0

ご支援いただき、ありがとうございます。しかし、配列は多くのNSMutableDictionaryオブジェクトで構成されています。したがって、これは当てはまりません。 – Ron

+0

正しい辞書を確認するための基準が必要で、それは何ですか? – icould

+0

NSString * dateKeySelected = @ "open_house_updated_date"; ' 'NSPredicate * predicateString = [NSPredicate predicateWithFormat:@"%K!= NULL "、dateKeySelected]; ' ' NSMutableDictionary * updatedDay = [[NSMutableDictionaryのalloc]のinit];; '' [updatedDayのsetObject:selectedDay forKey:@ "open_house_updated_date"://辞書はkey' 'にNSArray * filteredArray = [predicateString updatedDateAndTime filteredArrayUsingPredicate]を持ちます]; ' – Ron

関連する問題