1

I am using UISearchDisplayController to search and would like to add so more elements to each cell if that is possible. Did some searching but couldn't find any information. The only way is to reproduce it with tableview. Is there any other properties i can set other then textlabel.text

Thanks in advance!

Yan
  • 3,533
  • 4
  • 24
  • 45
  • Thank you very much for the responses! I was setting detailtextlabel.text and didn't show up so i thought its not possible to customize the cell. I tired with the imageview and it works. Btw is there a way to customize the cell in interface builder or i will have to programmatically create it? – Yan May 16 '12 at 15:03

3 Answers3

5

You can change the cell completely.

The following snippet relies on two subclasses of UITableCell, ANormalCell & ASearchCell.

Caveat: This code has not been compiled but you get the gist?

-(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;
}
Damo
  • 12,840
  • 3
  • 51
  • 62
  • Thanks! I am using storyboards. I don't think there is a way to create a nib with just a cell. I checked the storyboard and i don't see a way to customize uisearchdisplaycontroller cell there. – Yan May 16 '12 at 15:21
  • Storyboards and XIBs can be used together (in the same project) and designing custom cells using XIBs is something I personally do a lot. – Damo May 17 '12 at 07:35
  • I was having a similar issue and this fixes it. The difference is, it seems that search table wouldn't let me use custom cell via "table registerNib: forCellReuseIdentifier", but your suggestion ([[NSBundle mainBundle] loadNibNamed...) does! – David Carrico May 07 '15 at 18:46
3

Very possible. All of the tableView datasource methods are called with a tableView parameter. If

if (tableView == searchController.searchResultsTableView)

then alter the cell by adding subviews, or customizing properties. Same approaches apply for custom cells in this case as in any other.

danh
  • 62,181
  • 10
  • 95
  • 136
1

Yes you can set cell.imageView , cell.accessoryView , cell.BackGroundView .. etc.

read this UITableViewCell documentation for further information

Malek_Jundi
  • 6,140
  • 2
  • 27
  • 36