UISearchDisplayController无法正确显示自定义单元格

ter*_*wis 10 iphone objective-c uitableview uisearchdisplaycontroller ios

所以我有一个包含节和行的tableView,它使用自定义单元类.自定义单元格具有图像视图和一些标签.表视图工作正常,搜索工作,但搜索不显示我的自定义单元格类中的任何标签,只显示具有正确图像的imageView.我很困惑为什么会这样,特别是因为图像仍然显示,但不是标签.这是一些代码.

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


//TODO: problem with search view controller not displaying labels for the cell, needs fixing
JSBookCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

if(cell == nil) {
    cell = [[JSBookCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

JSBook *book = nil;
//uses the appropriate array to pull the data from if a search has been performed
if(tableView == self.searchDisplayController.searchResultsTableView) {
    book = self.filteredTableData[(NSUInteger)indexPath.section][(NSUInteger)indexPath.row];
}
else {
    book = self.books[(NSUInteger)indexPath.section][(NSUInteger)indexPath.row];
}

FFMetaData *data = [self.ff metaDataForObj:book];
cell.titleLabel.text = book.title;
cell.priceLabel.text = [NSString stringWithFormat:@"$%@", book.price];
cell.authorLabel.text = book.author;
cell.descriptionLabel.text = book.description;

cell.dateLabel.text = [self.formatter stringFromDate:data.createdAt];
if(book.thumbnail == nil) {
    cell.imageView.image = [UIImage imageNamed:@"messages.png"];
    [self setCellImage:cell withBook:book atIndex:indexPath withTableView:tableView];
}
else {
    cell.imageView.image = [UIImage imageWithData:book.thumbnail];
}

return cell;
Run Code Online (Sandbox Code Playgroud)

}

在此问题出现之前,我在tableView中只有一个部分,一切都运行良好.现在我有多个部分和行,搜索按照我的描述被破坏了.有任何想法吗?此外,因为[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];我以前有[self.tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];但现在,如果我使用它,当我尝试搜索时,我得到一个奇怪的例外:

NSInternalInconsistencyException',原因:'请求无效索引路径的rect(2个索引[1,1])'

所以这也让我感到困惑.谢谢您的帮助!

sve*_*ena 30

[self.tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 
Run Code Online (Sandbox Code Playgroud)

因为表视图单元格已注册到特定的表视图,因此无效.这不适用于您的搜索结果控制器表视图.你确实找到了这个并切换到:

[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
Run Code Online (Sandbox Code Playgroud)

这是正确的做法.

此外,在故事板中设计自定义单元格对于搜索结果控制器不起作用,因为您无法为搜索表视图设计单元格,仅用于主表视图.

是的,您可以像在此处一样为您的搜索表视图注册该类,

[self.searchDisplayController.searchResultsTableView registerClass:[JSBookCell class] forCellReuseIdentifier:CellIdentifier];
Run Code Online (Sandbox Code Playgroud)

但是,这不会包含您在故事板中自定义单元格中设计的任何内容.您必须以编程方式创建所有.

  • 好吧,我创建了一个Xib,只是将我当前的单元格复制到那里并调用`[self.searchDisplayController.searchResultsTableView registerNib:[UINib nibWithNibName:@"searchCell"bundle:[NSBundle mainBundle]] forCellReuseIdentifier:CellIdentifier];`它完美地工作了.谢谢! (16认同)
  • @terrylewis,在与特里讨论之后,我进一步研究了这个问题.你(斯文纳)在顶部所说的不正确.实际上你可以使用self.tableView,如果你想在故事板中设计你的单元格,那就是你想要的方式(因为那是故事板设计单元格所在的表格视图,你必须从那个表格中获取它) ).搜索显示控制器仍然使用自己的表视图,但它从主表视图(通过其标识符)获取单元格.我认为最初的问题与部分有关,而不是使用self.tableView. (5认同)

har*_*nsa 24

当我尝试搜索时,我得到了相同的异常,以及一些如何修复它.

    if (tableView == self.searchDisplayController.searchResultsTableView) {
        cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    }else{
       cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];     
    }
Run Code Online (Sandbox Code Playgroud)


Daz*_*ong 9

使用在故事板原型中设计的相同自定义单元格的UITableView与搜索栏和搜索显示的摘要:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"CellIdentifierAsYouDefinedInStoryboard";

    CustomTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    if (tableView == self.searchDisplayController.searchResultsTableView) {
        /* searchResultsTableView code here */
    } else {
        /* Base tableView table code here */
    }

    /* more cell code here */

    return cell;
}
Run Code Online (Sandbox Code Playgroud)


然后为searchResultsTableView添加此行以匹配您的自定义单元格高度:

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.searchDisplayController.searchResultsTableView setRowHeight:self.tableView.rowHeight];

     /* more of your viewDidLoad code */
}
Run Code Online (Sandbox Code Playgroud)

  • 大!最后正是我需要的.如果要重用该样式,则强调"self.tableView dequeueReusableCellWithIdentifier"而不是任何其他表视图. (2认同)

rha*_*vey 6

如果您在UIViewController中使用UITableView并且想要重新使用在StoryBoard中为searchDisplayController创建的单元标识符,请尝试以下操作:

StoryBoard> UIViewController> Reference Outlets>将tableView链接到你的UIViewController的.h文件并调用它像tableView,所以你应该有这样的东西:

@property (strong, nonatomic) IBOutlet UITableView *tableView;
Run Code Online (Sandbox Code Playgroud)

所以不要这样做:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]
}
Run Code Online (Sandbox Code Playgroud)

做这个

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]
}
Run Code Online (Sandbox Code Playgroud)