【发布时间】:2012-01-19 07:29:03
【问题描述】:
我正在将此代码用于搜索栏,但它没有显示任何结果。
在 viewdidload 方法中:
filteredListitems = [[NSMutableArray alloc] initWithArray:listVehicles];
搜索栏方法:
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
if ([searchText length] == 0) {
[filteredListitems removeAllObjects];
[filteredListitems addObjectsFromArray:listVehicles];
}else {
[filteredListitems removeAllObjects];
for (NSString * string in listVehicles) {
NSRange r = [string rangeOfString:searchText options:NSCaseInsensitiveSearch];
if (r.location != NSNotFound) {
[filteredListitems addObject:string];
}
}
}
[listTable reloadData];}
-(void)searchBarSearchButtonClicked:(UISearchBar *)aSearchBar {
[searchBar resignFirstResponder];
}
每个单元格的代码是:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"vlCell";
VehicleListCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSLog(@"Cell Created");
NSArray *nibObjects = [[NSBundle mainBundle] loadNibNamed:@"VehicleListCell" owner:nil options:nil];
for (id currentObject in nibObjects) {
if ([currentObject isKindOfClass:[VehicleListCell class]]) {
cell = (VehicleListCell *)currentObject;
}
}
UILongPressGestureRecognizer *pressRecongnizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(tableCellPressed:)];
pressRecongnizer.minimumPressDuration = 0.5f;
[cell addGestureRecognizer:pressRecongnizer];
[pressRecongnizer release];
}
cell.textLabel.font = [UIFont systemFontOfSize:10];
[[cell ignition] setImage:[UIImage imageNamed:@"ignition.png"]];
[[cell direction] setImage:[UIImage imageNamed:@"south.png"]];
NSString *cellValue = [filteredListitems objectAtIndex:indexPath.row];
cell.licPlate.text = cellValue;
return cell;
}
我已经尝试使用显示控制器搜索栏,它工作正常,但问题是它为过滤后的搜索结果显示了自己的表格视图,而我使用自定义单元格在表格中显示不同的列,如下所示:
我想在搜索时或搜索完成后看到与上面相同的视图
搜索后我得到了这个视图
查看表格视图之间的区别,因为搜索标题会消失,所以有人建议我只使用没有显示控制器的搜索栏。
请指导我,以便我解决此问题
【问题讨论】:
-
NSLog 你的数组过滤列表项在 if 语句中。
-
选中,搜索按钮不起作用,filteredListItems 数组显示了 listVehicles 的所有项目
-
如何在视图中添加搜索栏?
-
从对象库中拖放
-
是搜索栏还是 searchViewController ?你有没有提供参考?
标签: iphone objective-c ios xcode4.2