[英]Unable to dequeue a cell when searching table view parse.com iOS
I'm trying to do a search function in my table view, to return objects from my Parse.com class. 我正在尝试在表格视图中执行搜索功能,以从Parse.com类返回对象。
I'm getting this error when I try to make a search in the UISearchBar: 当我尝试在UISearchBar中进行搜索时出现此错误:
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'unable to dequeue a cell with identifier FeaturedTableViewCell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard' ***由于未捕获的异常“ NSInternalInconsistencyException”而终止应用程序,原因:“无法使标识符为FeaturedTableViewCell的单元出队-必须为该标识符注册一个笔尖或一个类,或者在情节提要中连接原型单元”
Here's how I'm doing it: 这是我的做法:
@interface DiscoverViewController () <UISearchBarDelegate, UISearchDisplayDelegate>
@property (nonatomic, retain) PFQuery *query;
@property (nonatomic, retain) PFObject *category;
@end
@implementation DailyDiscoverViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.searchController = [[UISearchDisplayController alloc] initWithSearchBar:self.searchBar contentsController:self];
self.searchController.searchResultsDataSource = self;
self.searchController.searchResultsDelegate = self;
self.searchController.delegate = self;
self.searchResults = [NSMutableArray array];
_categories = [[NSMutableArray alloc] initWithCapacity:100];
}
- (void)filterResults:(NSString *)searchTerm {
[self.searchResults removeAllObjects];
PFQuery *query = [PFQuery queryWithClassName:@"Projects"];
[query whereKeyExists:@"name"];
[query whereKey:@"name" containsString:searchTerm];
NSArray *results = [query findObjects];
[self.searchResults addObjectsFromArray:results];
}
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
[self filterResults:searchString];
return YES;
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
[searchBar resignFirstResponder];
}
- (void)refresh {
PFQuery *query = [PFQuery queryWithClassName:@"Categories"];
[query orderByDescending:@"name"];
[query findObjectsInBackgroundWithBlock:^(NSArray *posts, NSError *error) {
if (!error) {
//NSLog(@"%@", posts);
} else {
}
[_categories setArray:posts];
[self.tableView reloadData];
[_loadingView stopAnimating];
[_refreshControl endRefreshing];
}];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (tableView == self.tableView) {
if (section == 0) {
return 1;
} else {
return self.categories.count;
}
} else {
return self.searchResults.count;
}
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == 0) {
return 320;
} else {
return 52;
}
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 2;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == 0) {
_featuredObject = [_featured objectAtIndex:indexPath.row];
DiscoverFeaturedTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DiscoverFeaturedTableViewCell" forIndexPath:indexPath];
[(PFFile*)_featuredObject[@"picture"] getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
cell.image1.image = [UIImage imageWithData:data];
}];
return cell;
} else {
_category = [_categories objectAtIndex:indexPath.row];
DiscoverTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DiscoverTableViewCell" forIndexPath:indexPath];
cell.name.text = _category[@"name"];
if ([tableView isEqual:self.searchDisplayController.searchResultsTableView]) {
PFUser *obj2 = [self.searchResults objectAtIndex:indexPath.row];
PFQuery *query = [PFQuery queryWithClassName:@"Projects"];
PFObject *searchedUser = [query getObjectWithId:obj2.objectId];
NSString *first = [searchedUser objectForKey:@"name"];
cell.name.text = [first substringToIndex:1];
cell.name.text = first;
return cell;
}
return cell;
}
}
@end
Ok, 好,
Try it on viewDidLoad : 在viewDidLoad上尝试 :
YourCustomCell *yourCustomCell = [UINib nibWithNibName:@"YourCustomCell" bundle:nil];
YourCustomCell * yourCustomCell = [UINib nibWithNibName:@“ YourCustomCell”捆绑包:无]; [self.tableView registerNib:cellNib forCellReuseIdentifier:@"cell"];
[self.tableView registerNib:cellNib forCellReuseIdentifier:@“ cell”];
Note : Your custom cell must have a .xib of course. 注意 :当然,您的自定义单元格必须具有.xib。
You could try this, I remember running into this exact issue and this was one of the solutions I tried: [self.tableView registerClass:[DiscoverTableViewCell class] forCellReuseIdentifier:@"DiscoverTableViewCell"];
您可以尝试一下,我记得碰到了这个确切的问题,这是我尝试过的解决方案之一:
[self.tableView registerClass:[DiscoverTableViewCell class] forCellReuseIdentifier:@"DiscoverTableViewCell"];
However, I distinctly remember that this didn't work. 但是,我清楚地记得这是行不通的。 It turned out I hadn't correctly connected the cells in the storyboard.
原来我没有正确连接情节提要中的单元格。 Furthermore, make sure that the class of your storyboard cell has been changed to represent your cell's custom class.
此外,请确保已将情节提要单元的类更改为代表您单元的自定义类。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.