もっと良い解決策があります、フランソワ。NSDictionary
あなたから(ここではそれを呼び出しますmyDictionary
)、次のように作成します(NSArray
インターフェースファイルで宣言します):
NSArray *arrayForTableView;
次に、NSDictionary をロードした直後に、次の操作を行います。
arrayForTableView = [myDictionary allKeys]; // so you will have an array of all UserID's
今、あなたのtableView: cellForRowAtIndexPath:
方法では、次のようにすることができます:
cell.textLabel.text = [myDictionary objectForKey:[arraForTableView objectAtIndex:indexPath.row]];
そして、ユーザーがセルを選択したときにユーザー ID を渡したい場合は、次のtableView: didSelectRowAtIndexPath:
ようにします。
id userIDSelected = [arraForTableView objectAtIndex:indexPath.row];
次に、検索に従って配列をフィルタリングする場合は、次のarrayForTableView
ように NSDictionary を「スキャン」することで、単純に を再作成できます。
NSString *typedString;
NSMutableArray *arrayFiltered = [NSMutableArray array];
for (int i = 0; i < [[myDictionary allKeys] count]; i++)
{
if ([[myDictionary objectForKey:[[myDictionary allKeys] objectAtIndex:i]] rangeOfString:typedString].location != NSNotFound)
{
[arrayFiltered addObject:[[myDictionary allKeys] objectAtIndex:i]];
}
}
arrayForTableView = arrayFiltered;
この方法では、UITableView の dataSource とデリゲート メソッドを変更する必要さえありません。