Custom UITableViewCell in UISearchDisplayControlle

2019-07-10 05:00发布

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];
}

5条回答
The star\"
2楼-- · 2019-07-10 05:16

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

查看更多
爱情/是我丢掉的垃圾
3楼-- · 2019-07-10 05:16

When UISearchDisplayController takes control of the table to display the results, it will call

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

To display the cell, so the only thing you have to do is detect when the displayController is calling this method:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([tableView isMemberOfClass:[UITableView class]])
    //This is the table view
    else
    //This is the display controller

Do the same with the rest of tghe methods of the delegate/datasource to implemente the search results cells as you want (for example, height or number of rows)

查看更多
孤傲高冷的网名
4楼-- · 2019-07-10 05:18

On your instance of UISearchDisplayController (assuming it's called myUISearchDisplayController) call myUISearchDisplayController.searchResultsTableView.rowHeight = 30;

查看更多
仙女界的扛把子
5楼-- · 2019-07-10 05:20

If row height is the only thing to change. setting it with searchResultsTableView.rowHeight works only for the first time. Tapping cancel and search with new search key ignore the custom rowHeight. (iOS 6.1, xcode 4.6.1) use the below.

- (void)searchDisplayController:(UISearchDisplayController *)controller didShowSearchResultsTableView:(UITableView *)tableView {
    tableView.rowHeight = 50.0;
}
查看更多
\"骚年 ilove
6楼-- · 2019-07-10 05:32

Search display controller has it's own table in which it shows the data which is searching.You can set the height by using this code-

SDC.searchResultsTableView.rowHeight = 30;//whatever you want 

Where SDC is instance of UISearchDisplayController

查看更多
登录 后发表回答