Search code examples
iosuitableviewuisearchdisplaycontroller

Custom UITableViewCell in UISearchDisplayController


I have UITableView and UISearchDisplayController. UITableViewCell is a custom cell with rowHeight = 30:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    UILabel *lbTitle;
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        [cell setSelectionStyle:UITableViewCellSelectionStyleGray];

        lbTitle = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, tableView.rowHeight)];
        [lbTitle setTag:1];
        [lbTitle setFont:[UIFont fontWithName:@"Sinhala Sangam MN" size:14]];
        //[lbTitle setTextAlignment: NSTextAlignmentCenter];
        [cell addSubview:lbTitle];
    }
    else {
        lbTitle = (UILabel *)[cell viewWithTag:1];
    }
}

How to apply this cell style in UISearchDisplayController. Because when search is "active" UISearchDisplayController's cell looks like basic.

list filtered list

Thanks

SOLVED by the help of MarkM's answer (Is there a way to customize UISearchDisplayController cell):

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

UITableViewCell* cell = nil;
UILabel *lbTitle;
if (tableView != self.searchDisplayController.searchResultsTableView) {
    cell = [tableView dequeueReusableCellWithIdentifier:normalCellReuseIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:normalCellReuseIdentifier];
        [cell setSelectionStyle:UITableViewCellSelectionStyleGray];

        lbTitle = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, tableView.rowHeight)];
        [lbTitle setTag:1];
        [lbTitle setFont:[UIFont fontWithName:@"Sinhala Sangam MN" size:14]];
        //[lbTitle setTextAlignment: NSTextAlignmentCenter];
        [cell addSubview:lbTitle];
    }
    else lbTitle = (UILabel *)[cell viewWithTag:1];
} else {
    tableView.rowHeight = 30;
    cell = [tableView dequeueReusableCellWithIdentifier:searchCellReuseIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:searchCellReuseIdentifier];
        [cell setSelectionStyle:UITableViewCellSelectionStyleGray];

        lbTitle = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, tableView.rowHeight)];
        [lbTitle setTag:1];
        [lbTitle setFont:[UIFont fontWithName:@"Sinhala Sangam MN" size:14]];
        //[lbTitle setTextAlignment: NSTextAlignmentCenter];
        [cell addSubview:lbTitle];
    }
    else lbTitle = (UILabel *)[cell viewWithTag:1];
}

Solution

  • All of the same UITableView delegate methods are called with the searchResultsTableView. Please reference the following answer for a more detailed explanation.

    Is there a way to customize UISearchDisplayController cell