3

我在我的项目中添加了一个搜索栏,效果很好。您可以搜索(表视图的)对象,并且关系也有效。但搜索显示不显示结果名称。

例如:我没有以“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;

}

4

2 回答 2

0

如果更新的代码是整个tableView:cellForRowAtIndexPath:方法,那么您没有将carImageViewcarNameLabelcarSpeedLabel视图添加到您的单元格中。

在初始化新单元格时,您需要添加和配置这些子视图,包括使用适当的标签。您应该将视图添加到cell.contentView然后用于[cell.contentView viewWithTag:X]检索它们。

于 2013-04-06T22:20:40.877 回答
0

您正在尝试获取UILabel按值设置的 s tag,但您使用的是 stock UITableViewCell。尝试以下操作:

if (tableView == self.searchDisplayController.searchResultsTableView) {
    Car *search = [searchResults objectAtIndex:indexPath.row];
    cell.textLabel.text = search.name;
} else {
    Car *car = [cars objectAtIndex:indexPath.row];
    cell.imageView.image = [UIImage imageNamed:car.thumbnail];

    cell.textLabel.text = car.name;
    cell.detailTextLabel.text = car.carSpeed;
}

如果您不想使用UIView内置于 normal 中的标准 s,则UITableViewCell必须在创建新单元格的块中创建并添加自定义视图(并设置其tag属性)。

于 2013-04-06T22:22:24.463 回答