【问题标题】:UISearchBar with UITableView containing custom cells => blank cells带有包含自定义单元格的 UITableView 的 UISearchBar => 空白单元格
【发布时间】:2013-12-11 13:25:35
【问题描述】:
  • 我有一个包含自定义单元格的 UITableView。一切正常。但之后,我决定添加一个 searchBar 以...搜索!

  • 所以,我在我的 xib 文件的 UITableView 下的 “对象库” 中添加了一个 “搜索栏和搜索显示控制器”。 p>

  • 我为自定义单元格创建了一个特定的类:

    “CustomCell.h”

    @class CustomCell;
    
    @interface CustomCell : UITableViewCell
    {
    }
    
    @property (weak, nonatomic) IBOutlet UILabel *lblDate;
    @property (weak, nonatomic) IBOutlet UILabel *lblAuteur;
    
    @end
    

    “CustomCell.m”

    no interesting stuff
    

    “CustomCell.xib”

  • “事件” 类:

    @interface Event : NSObject
    {
    }
    
    @property (strong, nonatomic) NSString *desc;
    @property (strong, nonatomic) NSString *dateCreation;
    
    @end
    
  • 以及包含 UITableViewUISearchBar 的视图:

    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *cellIdentifier = @"cell";
    
        // Determinate the current event
        Event *currentEvent;
        if (tableView == self.searchDisplayController.searchResultsTableView)
            currentEvent = [filteredArray objectAtIndex:indexPath.row];
        else
            currentEvent = [notFilteredArray objectAtIndex:indexPath.row];
    
        CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
        if(cell == nil)
            cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    
        cell.lblAuteur.text = [currentEvenement desc];
        cell.lblDate.text = [currentEvenement dateCreation];
    
        return cell;
    }
    
  • 好的,现在我们可以解决我的问题了。加载 tableView 后,自定义单元格显示良好。但如果我使用 SearchBar 则不是:

    1. 如果有一个desc属性等于"foo"的事件,并且如果我在SearchBar中输入"bar",我收到 “No results” 消息。这很正常。
    2. 如果有一个desc属性等于"foo"的事件,并且如果我在SearchBar中输入"foo",单元格正在显示,但没有内容!我只是看到单元格的边界,lblDatelblAuteur 等于 nil

我为什么会有这种行为?非常感谢您的帮助...我确定 filteredArraynotFilteredArray 已正确填充(我检查了很多次)。说明搜索机制运行良好。

【问题讨论】:

    标签: ios iphone objective-c uitableview uisearchbar


    【解决方案1】:

    经过多次“搜索”(双关语),我发现了问题。

    当您使用原型单元格引用表格视图时,您不能依赖传入的表格视图,因为有时该表格视图是没有原型单元格的搜索表格视图。

    所以而不是...

    HomeTabCell *cell = (HomeTabCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    

    用原型单元格直接连接到表格视图替换上面的“tableview”

    HomeTabCell *cell = (HomeTabCell *)[self.HomeTableView dequeueReusableCellWithIdentifier:CellIdentifier];
    

    希望这会有所帮助!

    【讨论】:

    • 感谢您的回答。如果我再次需要此功能,我会与您联系。
    【解决方案2】:

    检查这些设置,如果没有

    self.searchDisplayController.searchResultsDelegate = self; self.searchDisplayController.searchResultsDataSource = self;

    试试这个,

    【讨论】:

    • 我已经在 “搜索显示控制器”“文件所有者” 之间建立了到 Interface Builder 的链接。反正我试过把这两行放到viewDidLoad方法里面,但是问题还是出在这里……
    【解决方案3】:

    对于寻求解决此问题的人,我们深表歉意... 我无法修复它,并且主承包商不再想要这个功能,所以我放弃了。

    【讨论】:

    • 我上面好像回答了你的问题,你能不能把“选择的答案”改一下来帮助以后的搜索者?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-13
    • 1970-01-01
    • 2012-12-06
    • 2011-11-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多