如何使searchResultsTableView使用自定义单元格而不是默认单元格

时间:2014-05-28 21:00:44

标签: ios cocoa-touch uitableview uisearchbar

我有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;

}

这就是结果视图的样子:

enter image description here

1 个答案:

答案 0 :(得分:2)

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

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

有关此方法的详细信息,请参阅UITableView的{​​{3}}。

相关问题