重新加载 searchResultsTableView 时自定义单元格错误
当我单击此按钮-[搜索]时,会出现弹出窗口。
如果我写下搜索词并单击“搜索”按钮,数据将填充到 NSMutableArray 中。
此搜索表使用“自定义单元格”。
我无法将“自定义单元格”与 NSMutableArray 中的数据结合起来。
重新加载“searchTableView”时出现错误。
我该怎么办?请帮帮我!~谢谢~
[错误]
2011-10-31 20:56:52.915 TheBE[1207:10403] abcd 2011-10-31 20:56:53.158 TheBE[1207:10403] * -[UISearchResultsTableView _createPreparedCellForGlobalRow:withIndexPath:]、/SourceCache/UIKit_Sim/UIKit-1912.3/UITableView.m:6072 中的断言失败 2011-10-31 20:56:53.159 TheBE[1207:10403] 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“UITableView dataSource 必须从以下位置返回一个单元格”表视图:cellForRowAtIndexPath:' ** 首先抛出调用堆栈: (0x12a2052 0x1753d0a 0x124aa78 0xc712db 0x381ee3 0x382589 0x36ddfd 0x37c851 0x327322 0x12a3e72 0x29ba92d 0x29c4827 0x294afa7 0x294cea6 0x294c580 0x12769ce 0x120d670 0x11d94f6 0x11d8db4 0x11d8ccb 0x1fae879 0x1fae93e 0x2e8a9b 0x1d68 0x1cc5 0x1)
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { //****** custom cell *********/ SearchContentCell *cell = (SearchContentCell *)[tableView dequeueReusableCellWithIdentifier:@"SearchCell"]; [cell.contentLabel setLineBreakMode:UILineBreakModeWordWrap]; [cell.contentLabel setMinimumFontSize:FONT_SIZE]; [cell.contentLabel setNumberOfLines:0]; [cell.contentLabel setFont:[UIFont systemFontOfSize:FONT_SIZE]]; [cell.contentLabel setTag:1]; ....... cell.verseLabel.text = @"Test2" ; cell.contentLabel.text = @"test"; ....... return cell; } -(void)mockSearch:(NSString*)searchString { [_data removeAllObjects]; [request setEntity:entityBible]; // //create predicate NSString *wildcardedString = [NSString stringWithFormat:@"*%@*",searchString]; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"content like %@", wildcardedString]; [request setPredicate:predicate]; NSError *error; NSMutableArray *mutableFetchResults = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy]; if (mutableFetchResults == nil) { // Handle the error; NSLog(@"eerror"); } _data = mutableFetchResults; [self.searchDisplayController.searchResultsTableView reloadData]; } -(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{ [self mockSearch:searchBar.text]; }
我认为错误部分:
[self.searchDisplayController.searchResultsTableView reloadData];
When I click this button -[Search], pop-up window appear .
If I write down search terms and click Search button, the data are filled in NSMutableArray.
This searchTable use 'custom cell'.
I can't combine 'custom cell' with The data in NSMutableArray.
get errors when reloaded 'searchTableView'.
How can I do? Please help me!~ Thank you~
[error]
2011-10-31 20:56:52.915 TheBE[1207:10403] abcd
2011-10-31 20:56:53.158 TheBE[1207:10403] * Assertion failure in -[UISearchResultsTableView _createPreparedCellForGlobalRow:withIndexPath:], /SourceCache/UIKit_Sim/UIKit-1912.3/UITableView.m:6072
2011-10-31 20:56:53.159 TheBE[1207:10403] Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'
** First throw call stack:
(0x12a2052 0x1753d0a 0x124aa78 0xc712db 0x381ee3 0x382589 0x36ddfd 0x37c851 0x327322 0x12a3e72 0x29ba92d 0x29c4827 0x294afa7 0x294cea6 0x294c580 0x12769ce 0x120d670 0x11d94f6 0x11d8db4 0x11d8ccb 0x1fae879 0x1fae93e 0x2e8a9b 0x1d68 0x1cc5 0x1)
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { //****** custom cell *********/ SearchContentCell *cell = (SearchContentCell *)[tableView dequeueReusableCellWithIdentifier:@"SearchCell"]; [cell.contentLabel setLineBreakMode:UILineBreakModeWordWrap]; [cell.contentLabel setMinimumFontSize:FONT_SIZE]; [cell.contentLabel setNumberOfLines:0]; [cell.contentLabel setFont:[UIFont systemFontOfSize:FONT_SIZE]]; [cell.contentLabel setTag:1]; ....... cell.verseLabel.text = @"Test2" ; cell.contentLabel.text = @"test"; ....... return cell; } -(void)mockSearch:(NSString*)searchString { [_data removeAllObjects]; [request setEntity:entityBible]; // //create predicate NSString *wildcardedString = [NSString stringWithFormat:@"*%@*",searchString]; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"content like %@", wildcardedString]; [request setPredicate:predicate]; NSError *error; NSMutableArray *mutableFetchResults = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy]; if (mutableFetchResults == nil) { // Handle the error; NSLog(@"eerror"); } _data = mutableFetchResults; [self.searchDisplayController.searchResultsTableView reloadData]; } -(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{ [self mockSearch:searchBar.text]; }
I think error part :
[self.searchDisplayController.searchResultsTableView reloadData];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果这确实是您的
tableView:cellForRowAtIndexPath:
方法的样子,那么如果dequeueReusableCellWithIdentifier:
返回 nil,您就错过了分配新单元格的部分。If that's really what your
tableView:cellForRowAtIndexPath:
method looks like, you're missing the part that allocates a new cell ifdequeueReusableCellWithIdentifier:
returns nil.我有同样的错误。我认为这是因为调整某些按钮属性意味着需要设置视图(显然是延迟创建的),这意味着表需要准备好运行,但还没有,可能是因为其数据源尚未连接或其他什么。尝试将按钮调整移到 viewDidLoad 方法的后面;这对我有用。
(另请参阅 UITableView 在 viewDidLoad 中间初始化? ,其中我发布了相同的答案。)
I had the same error. I think it is because adjusting certain button properties means the view (apparently lazily created) needs to be set up, which means the table needs to be ready to go, and it isn't yet, perhaps because its datasource hasn't been connected or something. Try moving your button adjustment to later in the viewDidLoad method; that worked for me.
(See also UITableView getting Initialized in the middle of viewDidLoad?, wherein I posted the same answer.)
故事板中的自定义单元格,而不是“self.searchDisplayController.searchResultsTableView”中的自定义单元格,因此只需更改:
SearchContentCell *cell = (SearchContentCell *)[ -->tableView<-- dequeueReusableCellWithIdentifier:@"SearchCell"];
to
SearchContentCell *cell = (SearchContentCell *)[ -->self.tableView<--dequeueReusableCellWithIdentifier:@"SearchCell"];
custom cell in storyboard, not in "self.searchDisplayController.searchResultsTableView", so just change:
SearchContentCell *cell = (SearchContentCell *)[ -->tableView<-- dequeueReusableCellWithIdentifier:@"SearchCell"];
to
SearchContentCell *cell = (SearchContentCell *)[ -->self.tableView<--dequeueReusableCellWithIdentifier:@"SearchCell"];