自定义UISearchBar结果单元格

时间:2014-03-17 11:41:15

标签: ios cocoa-touch uitableview uisearchbar

我试图显示自定义UISearchBar结果单元格。我创建了一个自定义UITableViewCell来显示信息,但我想自定义UISearchBar结果。

我的课程中有这个代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"PodsCell";
    LinhaPod *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[LinhaPod alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }

    if (isSearching && [self.searchResults count])
    {
        NSString *texto = [self.searchResults objectAtIndex:indexPath.row];
        NSArray *arrTexto = [texto componentsSeparatedByString:@"#"];
        cell.titulo.text = arrTexto[6];
        cell.dataPod.text = [NSString stringWithFormat:@"%@ - %@", arrTexto[1], [arrTexto[3] stringByReplacingOccurrencesOfString:@"." withString:@"'"]];
        [cell setAccessoryType: UITableViewCellAccessoryDisclosureIndicator];
    }
    return cell;
}

此代码无效。我用"cell.textLabel.text = @"test";"进行了测试并且工作了。

我正在使用搜索栏和搜索显示控制器。

这是我UITableViewCell enter image description here

2 个答案:

答案 0 :(得分:1)

您是否已将UILabels添加到UITableViewCell? LinhaPod.m中的initWithStyle应该如下所示。 (假设您没有为此自定义单元格使用xib文件)

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {

        _dataPod = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 160, 44)];
        [self addSubview:_dataPod];

        _titulo = [[UILabel alloc]initWithFrame:CGRectMake(160, 0, 160, 44)];
        [self addSubview:_titulo];

    }
    return self;
}

当然,在你的LinhaPod.h中你也可以

@interface LinhaPod : UITableViewCell

@property(nonatomic) UILabel *titulo;
@property(nonatomic) UILabel *dataPod;

答案 1 :(得分:0)

- (void)searchDisplayController:(UISearchDisplayController *)controller willShowSearchResultsTableView:(UITableView *)tableView
{
    [tableView registerClass:[MCSelectCategoryTableViewCell_iPhone class] forCellReuseIdentifier:@"selectCategoryCell"];
}

如果你想在searchDisplayController的tableView中使用自定义单元格,你可以通过在searchDisplayController.searchDisplayTableView中注册它来注册storyBoard的任何UITableView子类。

相关问题