【发布时间】:2011-08-17 07:54:52
【问题描述】:
好的,我正在努力在具有部分的 tableView 上实现 UISearchBar。这可能是错误的,但是为了第一次填充表格视图,我有一个包含很多条目的数组,然后填充如下所示的部分:
if(indexPath.section ==0){
[cell.textLabel setText:[tableData objectAtIndex:indexPath.row]];
}else if(indexPath.section ==1){
[cell.textLabel setText:[tableData objectAtIndex:indexPath.row+4]];
}else if(indexPath.section ==2){
[cell.textLabel setText:[tableData objectAtIndex:indexPath.row+8]];
}
这远非优雅,但它确实有效。现在我正在尝试连接 UISearchBar,这是我遇到问题的方法:
- (void)searchBar:(UISearchBar *)sBar textDidChange:(NSString *)searchText
{
[tableData removeAllObjects];// remove all data that belongs to previous search
if([searchText isEqualToString:@""] || searchText==nil){
[tableView reloadData];
return;
}
NSInteger counter = 0;
for(NSString *name in dataSource)
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];
NSRange r = [name rangeOfString:searchText];
if(r.location != NSNotFound)
{
[tableData addObject:name];
}
[pool release];
}
[tableView reloadData];
}
因此,我再次创建了一个符合搜索条件的条目数组,但是当我尝试重新加载我的 tableView 时,它会因为期待部分而变得混乱。但我想要的只是一个普通的无节 tableView 中的结果。
如何使用带有部分的 tableView 实现这个 UISearchBar?
谢谢
【问题讨论】:
-
为不同的部分保留不同的数组。
标签: iphone uitableview uisearchbar reload