我正在尝试在表视图中执行搜索功能,以从 Parse.com 类中返回对象。
当我尝试在UISearchBar中进行搜索时,我收到此错误:
由于未捕获的异常"NSInternalInconsistencyException"而终止应用程序,原因:"无法使用标识符 FeaturedTableViewCell 取消排队的单元格 - 必须为标识符注册笔尖或类,或者在情节提要中连接原型单元格"
这是我的做法:
@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
你可以看看这个堆栈在流程帖子上:POST
K.
好的,
在viewDidLoad上尝试一下:
YourCustomCell *yourCustomCell = [UINib nibWithNibName:@"YourCustomCell" bundle:nil];[自我表视图 registerNib:cellNib forCellReuseIdentifier:@"cell"];
注意:当然,您的自定义单元格必须具有.xib。
你可以试试这个,我记得遇到了这个确切的问题,这是我尝试过的解决方案之一: [self.tableView registerClass:[DiscoverTableViewCell class] forCellReuseIdentifier:@"DiscoverTableViewCell"];
但是,我清楚地记得这不起作用。事实证明,我没有正确连接故事板中的单元格。此外,请确保情节提要单元格的类已更改为表示单元格的自定义类。