【发布时间】:2017-04-22 08:29:41
【问题描述】:
我想通过将图像放在文本旁边来搜索我的 UITableView。我无法理解这个问题,我说你一输入文本就会发生崩溃。
这是我的代码:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
if (isSearching) {
return [filteredContentList count];
}
else {
return [contentList count];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
if (isSearching) {
NSDictionary *searchResult = [filteredContentList objectAtIndex:indexPath.row];
NSString *uppercaseString = [[searchResult objectForKey:@"title"] uppercaseString];
cell.nameLabel.text = uppercaseString;
NSString *stringImage = [searchResult valueForKeyPath:@"thumbnail.file"];
[cell.parallaxImage sd_setImageWithURL:[NSURL URLWithString:stringImage]
placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
[cell.parallaxImage setShowActivityIndicatorView:YES];
[cell.parallaxImage setIndicatorStyle:UIActivityIndicatorViewStyleGray];
}
else {
NSDictionary *searchResult = [contentList objectAtIndex:indexPath.row];
NSString *uppercaseString = [[searchResult objectForKey:@"title"] uppercaseString];
cell.nameLabel.text = uppercaseString;
NSString *stringImage = [searchResult valueForKeyPath:@"thumbnail.file"];
[cell.parallaxImage sd_setImageWithURL:[NSURL URLWithString:stringImage]
placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
[cell.parallaxImage setShowActivityIndicatorView:YES];
[cell.parallaxImage setIndicatorStyle:UIActivityIndicatorViewStyleGray];
}
return cell;
}
- (void)searchTableList {
NSLog(@"title");
NSString *searchText = searchBar.text;
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"title CONTAINS[cd] %@", searchText];
filteredContentList = [contentList filteredArrayUsingPredicate:predicate];
NSLog(@"filtered : %@", filteredContentList);
}
#pragma mark - Search Implementation
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
isSearching = YES;
}
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
NSLog(@"Text change - %d",isSearching);
//Remove all objects first.
//[filteredContentList removeAllObjects];
[self searchTableList];
if([searchText length] != 0) {
isSearching = YES;
[self searchTableList];
}
else {
isSearching = NO;
}
[self.tblContentList reloadData];
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
NSLog(@"Cancel clicked");
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
NSLog(@"Search Clicked");
[self searchTableList];
}
(在我的代码中,NSPredicate 功能齐全,但我崩溃了 :)
*** Assertion failure in -[UISearchResultsTableView _configureCellForDisplay:forIndexPath:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3600.7.47/UITableView.m:8174
2017-04-22 10:24:59.538 TableSearch[13649:2676593] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView (<UISearchResultsTableView: 0x7d444000; frame = (0 0; 375 667); clipsToBounds = YES; autoresize = W+H; gestureRecognizers = <NSArray: 0x7de362c0>; layer = <CALayer: 0x7de35f50>; contentOffset: {0, -64}; contentSize: {375, 1936}>) failed to obtain a cell from its dataSource (<ViewController: 0x7dc37b00>)'
*** First throw call stack:
/////////
/////////
libc++abi.dylib: terminating with uncaught exception of type NSException
【问题讨论】:
-
据我从崩溃报告中可以看出,问题出在
[UISearchResultsTableView _configureCellForDisplay:forIndexPath:]内。你重写了这个方法吗?如果是这样,你能指定它的代码吗? -
@AleksandrMedvedev 我所有的代码都打开了,我错过了什么?
-
第一个 - 一个小建议:因为你有两种状态,搜索和非搜索,并且单元格的设置是相同的,你可以这样:NSArray * datasource = isSearching?过滤内容列表:内容列表;然后使用数据源配置单元格。这将减少您拥有的代码量。第二个问题:在搜索和不搜索之间切换时是否调用了[tableView reloadData]?
-
这里是解决方案,请查看链接。 stackoverflow.com/questions/36503790/…>
标签: ios objective-c uitableview crash searchbar