【问题标题】:IOS SearchDisplayController with Custom Cell StoryBoard带有自定义单元故事板的 IOS SearchDisplayController
【发布时间】:2013-11-04 17:17:38
【问题描述】:

我在情节提要中有一个单元原型。所以我自定义了高度、子视图(标签和图像)。 但最终该单元格似乎并未用于 SearchDisplayController ...... 代码sn-p >>

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
#warning Reusable cell not working for custom cell.
ItemCell *cell = [tableView dequeueReusableCellWithIdentifier:itemCell];

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

if (tableView == self.searchDisplayController.searchResultsTableView){
    cell.itemLabel.text = [_filteredList objectAtIndex:indexPath.row];
    cell.priceLabel.text = @"RM 7.00";
    // TODO : Insert Image Here
} else {
    cell.itemLabel.text = [_items objectAtIndex:indexPath.row];
    cell.priceLabel.text = @"RM 7.00";
    // TODO : Insert Image Here
}

return cell;}

我想不通。即使我使用 self.tableView 而不是 tableView 它也会显示类似的内容。

【问题讨论】:

  • 您确定您的单元格标识符在您的故事板中设置正确吗? (与 itemCell 的值相同)
  • 确定单元格标识符设置正确。我再次确认。
  • 是单元格大小不对还是搜索后没有填入数据的问题?
  • 它是单元格大小。搜索显示控制器的单元格不符合我的原型单元格大小

标签: ios iphone objective-c uitableview


【解决方案1】:

在行方法的单元格之前添加以下代码。

 - (void)searchDisplayController:(UISearchDisplayController *)controller  didLoadSearchResultsTableView:(UITableView *)tableView
 {
tableView.rowHeight = 24.0f; // this should be the same hight as the re usable cell you implemented 
  }

然后当表格视图加载并执行搜索时,搜索控制器的单元格高度和表格视图将是相同的。

【讨论】:

    【解决方案2】:

    您应该使用此方法进行进一步的更改,例如:

    -(BOOL)searchDisplayController:(UISearchDisplayController *)controller  shouldReloadTableForSearchString:(NSString *)searchString
    {
    
    [controller.searchResultsTableView setBackgroundColor:[UIColor colorWithRed:0xED/255.0 green:0xED/255.0 blue:0xED/255.0 alpha:1.0]];
    
    controller.searchResultsTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    return YES;
    }
    

    【讨论】:

    • 但这对于可重复使用的单元来说可能是一项双重工作?我已经在情节提要中设置了一次,我应该期望我可以重复使用它们,对吗?
    猜你喜欢
    • 2016-04-24
    • 1970-01-01
    • 1970-01-01
    • 2013-01-19
    • 2013-10-14
    • 1970-01-01
    • 2017-01-03
    • 2012-01-08
    • 2012-09-26
    相关资源
    最近更新 更多