2012-10-16 49 views
0

我有這樣this.`如果tableview從字典中加載,如何實現搜索欄?

NSDictionary *memberInfo = [self.currentChannel infoForMemberWithID:memberID]; 

    memberinfo=memberInfo; 

    cell.textLabel.text = [NSString stringWithFormat:@"%@",[memberinfo objectForKey:@"name"]]; 
    cell.detailTextLabel.text = [NSString stringWithFormat:@"%@",[memberinfo objectForKey:@"status"]]; 


    cell.textLabel.backgroundColor = [UIColor clearColor]; 
    cell.detailTextLabel.backgroundColor = [UIColor clearColor]; 

    return cell; 

`表格視圖,將項目添加到這一點,這是工作fine.Now我想添加一個搜索欄,以that.i需要根據加載的tableview匹配string.But我加載像this.I的實現代碼如下知道如何在array.I搜索正在使用

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText.i want to load the table view according to the match of the searchstring ,Can anybody know this? 
+0

你有多少行在這裏..如何保持這一點。 – vishy

+0

@vishy的意思?我有成員算作行號 – hacker

+0

意味着你有n個對象的字典,和n個行..? – vishy

回答

0

您可以使用NSPredicate你的搜索欄裏面委派到主陣列(成員過濾或任何你命名它)並在另一個全球聲明的數組中捕獲結果。現在用這個過濾的數組重新加載你的表。

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText 
{ 
    [_mArrFilteredList removeAllObjects]; //global array which will contain filtered results 
    NSString* searchStr = [NSString stringWithFormat:@"*%@*",_srbActivity.text]; 

    NSPredicate* predicate = [NSPredicate predicateWithFormat:@"%K == %@",@"name", searchStr]; //will filter according to "name" key in your dictionary. 
    [_mArrFilteredList addObjectsFromArray:[_mArrList filteredArrayUsingPredicate:predicate]]; //_mArrList is your main array which has all the dictionaries. 
    [yourTableView reloadData]; 
} 

現在在你的tableview數據源方法中,使用過濾數組(_mArrFilteredList)填充表。

0

對於任何搜索實現,我們需要兩個列表,一個原始和其他過濾。我們通常會使用過濾列表來填滿表格。

就你而言,你可以將字典作爲原始列表。對於已過濾的列表,您可以根據需要使用值或密鑰的NSArray

在搜索功能中過濾數組列表,重新載入表格。在cellForRow從數組鍵中獲取對象或從數組值中獲取鍵 - 稍後將該鍵用作對象。