UISearchDisplayController 中的自定义 UITableViewCell [英] Custom UITableViewCell in UISearchDisplayController

查看:34
本文介绍了UISearchDisplayController 中的自定义 UITableViewCell的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 UITableViewUISearchDisplayController.UITableViewCell 是一个具有 rowHeight = 30 的自定义单元格:

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

如何在 UISearchDisplayController 中应用这种单元格样式.因为当搜索处于活动状态"时,UISearchDisplayController 的单元格看起来很基本.

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

谢谢

解决在 MarkM 的回答的帮助下(有没有办法自定义 UISearchDisplayController 单元格):

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

推荐答案

使用 searchResultsTableView 调用所有相同的 UITableView 委托方法.请参考以下答案以获得更详细的解释.

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

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

这篇关于UISearchDisplayController 中的自定义 UITableViewCell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆