我正在從數組中填充我的表格視圖。而這個數組是由SQLite查詢創建的。
所以在我的數組中,我有來自我的用戶定義的類的對象。
而我在我的表中使用自定義單元格。像object.name
在一層,object.id
附近這層。 到目前爲止一切都很好。但是如果我嘗試使用UISearchBar,我將如何重新填充我的表格?如何在UITableView中使用UISearchBar定製單元格
這是我創建我的表的代碼。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"SpeakersCell";
SpeakersCell *cell = (SpeakersCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"SpeakersCell" owner:self options:nil];
for (id currentObject in topLevelObjects) {
if ([currentObject isKindOfClass:[SpeakersCell class]]) {
cell = (SpeakersCell *) currentObject;
break;
}
}
}
// Set up the cell
ProjectAppDelegate *appDelegate = (ProjectAppDelegate *)[[UIApplication sharedApplication] delegate];
Speaker *speakerObject = (Speaker *)[appDelegate.speakers objectAtIndex:indexPath.row];
cell.lblSpeakerName.text = speaker.speakerName;
cell.lblSpeakerCity.text = speaker.speakerCity;
return cell;
}
當我添加一個搜索欄,並定義searchBar textDidChange
情況下,我順利拿到包含密鑰字母的項目。但我無法重新填充我的表格,因爲我只搜索了項目的名稱。
教程我從默認單元格獲得幫助,並且tableview的數據源是NSString數組。但我的數組是NSObject數組,這是我的問題。
我考慮獲取搜索項目的索引,但我怎樣才能使用這些值呢?你有什麼想法或任何相關的鏈接?
好吧,我意識到使用UISearchDisplayContoller更容易,但我找不到直接的教程。我會嘗試並告知這裏的結果。謝謝一堆。 – kubilay
由於某種原因,我採取了另一種方式,但再次感謝您的答案。 – kubilay
歡迎您。你能分享你跟蹤的方式嗎? –