Вопрос

I'm trying to display a custom UISearchBar Results Cell. I created a custom UITableViewCell to display the information but I want to customize the UISearchBar results.

I have this code in my class:

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

This code is not working. I tested with "cell.textLabel.text = @"test";" and worked.

I'm using Search Bar and Search Display Controller.

This is my UITableViewCell enter image description here

Это было полезно?

Решение

Have you added the UILabels to your UITableViewCell ? Your initWithStyle in LinhaPod.m should look like this snippet below. (Assuming your are not using a xib file for this custom cell)

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

Of course, in your LinhaPod.h you would also have

@interface LinhaPod : UITableViewCell

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

Другие советы

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

If you want to use a custom cell in the tableView of the searchDisplayController you could register any of the UITableView subclasses of your storyBoard by registering it like this in the searchDisplayController.searchDisplayTableView.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top