我在我的项目中添加了一个搜索栏,效果很好。您可以搜索(表视图的)对象,并且关系也有效。但搜索显示不显示结果名称。
例如:我没有以“x”开头的对象>>>所以没有结果(这是正确的):
但是一个对象以“b”开头,但是虽然我可以单击它并没有显示名称,但它正确显示了下一个视图(关系):
也许这是由于我的代码引起的:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *simpleTableIdentifier = @"CarCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
if (tableView == self.searchDisplayController.searchResultsTableView) {
Car *search = [searchResults objectAtIndex:indexPath.row];
UILabel *carNameLabel = (UILabel *)[cell viewWithTag:101];
carNameLabel.text = search.name;
我不知道为什么这不起作用,这对我来说似乎很奇怪。如果有人可以帮助我,那就太好了。
更新:完整方法
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *simpleTableIdentifier = @"CarCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
if (tableView == self.searchDisplayController.searchResultsTableView) {
Car *search = [searchResults objectAtIndex:indexPath.row];
UILabel *carNameLabel = (UILabel *)[cell viewWithTag:101];
carNameLabel.text = search.name;
} else {
Car *car = [cars objectAtIndex:indexPath.row];
UIImageView *carImageView = (UIImageView *)[cell viewWithTag:100];
carImageView.image = [UIImage imageNamed:car.thumbnail];
UILabel *carNameLabel = (UILabel *)[cell viewWithTag:101];
carNameLabel.text = car.name;
UILabel *carSpeedLabel = (UILabel *)[cell viewWithTag:102];
carSpeedLabel.text = car.carSpeed;
}
return cell;
}