【发布时间】:2014-02-06 00:12:59
【问题描述】:
我创建了一个搜索 tableview 内容的 Search 方法。这很好用,每次我在搜索栏中写入时,它都会将对象添加到一个名为 filtersArray 的数组中,并显示等于 filtersArray.count 的 tableviewcells 的数量。我已经用 NSLog(@"%@", filteredArray);
对此进行了测试问题是当你搜索时我想在表格视图中显示过滤数组。我已经为此更改了 cellForRowAtIndexPath 方法,但它只是给了我空的 tableviewcells。我做错了什么?
不搜索:
搜索“ru”:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
LanguageCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[LanguageCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
if (tableView == self.tableViewData) {
cell.patternLabel.text = [NSString stringWithFormat:@"%@", [[rows objectAtIndex:indexPath.row]objectAtIndex:0]];
} else{
cell.patternLabel.text = [NSString stringWithFormat:@"%@", [filteredArray objectAtIndex:indexPath.row]];
}
return cell;
}
搜索方法:
-(void) searchThroughdata {
self.filteredArray = nil;
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF contains [c] %@",self.searchBar.text];
self.filteredArray = [[finalArray filteredArrayUsingPredicate:predicate] mutableCopy];
NSLog(@"%@", filteredArray);
}
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
[self searchThroughdata];
}
【问题讨论】:
-
显示您的搜索委托方法代码。
-
完成。我添加了方法和 2 张图片来说明我什么时候不搜索和什么时候搜索。
-
LanguageCell- 单元格是否已在表格中注册?cell = [[LanguageCell alloc] initWithStyle:...何时运行? -
它注册在一个名为 UITableViewCell 的类中。 languageCell.h 和 .m
-
这不是注册的意思(即定义)。调试并告诉我
cell = [[LanguageCell alloc] initWithStyle:...何时运行。
标签: ios objective-c uitableview uisearchbar