有没有办法自定义UISearchDisplayController单元格

时间:2012-05-16 14:11:29

标签: objective-c ios

我正在使用UISearchDisplayController进行搜索,并希望在每个单元格中添加更多元素(如果可能的话)。做了一些搜索但找不到任何信息。唯一的方法是使用tableview重现它。是否还有其他属性可以设置为textlabel.text

提前致谢!

3 个答案:

答案 0 :(得分:5)

您可以完全更改单元格。

以下片段依赖于UITableCell的两个子类,ANormalCell& ASearchCell。

警告:这段代码尚未编译,但你得到了要点?

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *normalCellReuseIdentifier = @"ANormalCell";
    static NSString *searchCellReuseIdentifier = @"ASearchCell";

    UITableViewCell* cell = nil;

    if (tableView != self.searchDisplayController.searchResultsTableView) {
        cell = (ANormalCell*)[self.tableView dequeueReusableCellWithIdentifier:normalCellReuseIdentifier];

        if (cell == nil) {
            NSArray* topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"ANormalCell" owner:nil options:nil];
            cell = (ANormalCell*)[topLevelObjects objectAtIndex:0];
        }
    } else {
        cell = (ASearchCell*)[self.tableView dequeueReusableCellWithIdentifier:searchCellReuseIdentifier];

        if (cell == nil) {
            NSArray* topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"ASearchCell" owner:nil options:nil];
            cell = (ASearchCell*)[topLevelObjects objectAtIndex:0];
        }
    }

    return cell;
}

答案 1 :(得分:3)

非常可能。使用tableView参数调用所有tableView数据源方法。如果

if (tableView == searchController.searchResultsTableView)

然后通过添加子视图或自定义属性来更改单元格。在这种情况下,相同的方法适用于任何其他的自定义单元格。

答案 2 :(得分:1)

是的,您可以设置cell.imageView,cell.accessoryView,cell.BackGroundView ..等。

阅读此UITableViewCell文档以获取更多信息