【发布时间】: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 -
以及包含 UITableView 和 UISearchBar 的视图:
-(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 则不是:
- 如果有一个desc属性等于"foo"的事件,并且如果我在SearchBar中输入"bar",我收到 “No results” 消息。这很正常。
- 如果有一个desc属性等于"foo"的事件,并且如果我在SearchBar中输入"foo",单元格正在显示,但没有内容!我只是看到单元格的边界,lblDate 和 lblAuteur 等于 nil。
我为什么会有这种行为?非常感谢您的帮助...我确定 filteredArray 和 notFilteredArray 已正确填充(我检查了很多次)。说明搜索机制运行良好。
【问题讨论】:
标签: ios iphone objective-c uitableview uisearchbar