有一個更好的解決方案,弗朗索瓦。 創建,從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]];
然後,當喲U將要傳遞的用戶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數據源和委託方法。
好的,我明白了你的觀點。我從錯誤的方面解決了問題,並且我被相同價值觀的非問題所打擾。謝謝你的幫助。 – 2013-04-28 10:55:04
我很高興我的幫助。如果它解決了你的問題,也請投票:) – 2013-04-28 10:57:43
還沒有足夠的聲望做到這一點,但我會:) – 2013-04-28 11:06:21