【问题标题】:how to make searchResultsTableView use custom cell instead of default cell [duplicate]如何使searchResultsTableView使用自定义单元格而不是默认单元格[重复]
【发布时间】:2014-05-28 21:00:44
【问题描述】:

我有一个 uisearchbar 可以成功进行搜索。问题是searchResultsTableView 没有显示结果,因为它使用的是常规UITableViewCell 而不是我的自定义单元格。我已经在故事板中设置了这个自定义单元并连接了插座以确保一切正常。

我的cellForRowAtIndexPath 方法如下所示:

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


    static NSString *CellIdentifier = @"InviteCell";

    InviteTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if ( cell == nil ) {
        cell = [[InviteTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }

    MyUser *person = nil;

    if (tableView == self.searchDisplayController.searchResultsTableView)
    {

        person = [self.filteredContactArray objectAtIndex:[indexPath row]];
        //This works and shows
        cell.detailTextLabel.text = @"testing if using default cell";

    }
    else
    {
        person = [self.inviteContactsArray objectAtIndex:[indexPath row]];
    }




    //InviteTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    person = [self.inviteContactsArray objectAtIndex:indexPath.row];

    //these do not work and do not show in the results view
    cell.nameLabel.text = person.fullName;
    cell.emailLabel.text = person.email;

    return cell;

}

结果视图如下所示:

【问题讨论】:

标签: ios cocoa-touch uitableview uisearchbar


【解决方案1】:

目前无法将在故事板中创建的原型单元格注册到searchResultsTableView。

解决方法是将单元格放入单独的 xib 文件中,并使用 -[UITableView registerNib:forCellReuseIdentifier:] 为 self.tableView 和 self.searchDisplayController.searchResultsTableView 注册它。

有关此方法的更多信息,请参阅UITableView 的documentation。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-11-25
    • 2015-04-01
    • 2013-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-29
    相关资源
    最近更新 更多