[英]How to Use UISearchBar for Custom Cells in UITableView
I'm populating my tableview from an array. 我正在从一个数组中填充我的tableview。 And this array is created by SQLite query. 这个数组是由SQLite查询创建的。
So in my array, I have objects from my user-defined class. 所以在我的数组中,我有来自用户定义类的对象。
And I use custom cells in my table. 我在桌子上使用自定义单元格。 Like object.name
in one layer, object.id
near this layer. 与object.name
中的object.id
一样,此层附近的object.id
。 So far everything's good. 到目前为止一切都很好。 But if I try to use UISearchBar, how will I re-populate my table? 但是,如果我尝试使用UISearchBar,我将如何重新填充表格?
This is the code I create my table. 这是我创建表的代码。
- (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;
}
When I add a search bar and define searchBar textDidChange
event, I successfully get items that contains the key letters. 当我添加搜索栏并定义searchBar textDidChange
事件时,我成功获取包含关键字母的项目。 But I can't re-populate my table because I only have searched items' names. 但我不能重新填充我的表,因为我只搜索项目的名称。
Tutorials I got help from are built on default cells and the datasource of tableview is NSString array. 我得到帮助的教程是基于默认单元格而tableview的数据源是NSString数组。 But my array is NSObject array, this is my problem. 但我的数组是NSObject数组,这是我的问题。
I consider getting indexes of searched items but how can I use these values either? 我考虑获取搜索项目的索引,但我如何使用这些值呢? Do you have any idea or any related link? 您有任何想法或相关链接吗?
I think you should take a look at Bobgreen's blog, his tutorial helped me quite a lot. 我想你应该看看Bobgreen的博客,他的教程对我帮助很大。
http://www.cocoabob.net/?p=67 http://www.cocoabob.net/?p=67
Check his delegeate function for UISearchDisplayController (which I guess you might want to use?) 检查他的UISearchDisplayController的删除功能(我想你可能想使用它?)
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
[self.filteredListContent removeAllObjects];
for (Product *product in listContent)
{
if ([scope isEqualToString:@"All"] || [product.type isEqualToString:scope])
{
NSComparisonResult result = [product.name compare:searchText options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0, [searchText length])];
if (result == NSOrderedSame)
{
[self.filteredListContent addObject:product];
}
}
}
}
He has an array of NSObject's ( product
) there which he loops thru. 他有一系列NSObject( product
),他在那里循环。 Each time a result matches the search string he puts the value in an second array called filteredListContent
and he'll show upp the filtered content. 每次结果与搜索字符串匹配时,他都会将该值放在名为filteredListContent
的第二个数组中,并且他将向过滤的内容显示upp。 Works like a charm for me. 对我来说就像一个魅力。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.