搜索表视图parse.com iOS时无法使单元格出列

时间:2015-04-16 14:02:51

标签: ios objective-c uitableview parse-platform uisearchdisplaycontroller

我试图在我的表视图中执行搜索功能,从Parse.com类中返回对象。

当我尝试在UISearchBar中进行搜索时,我收到此错误:

***由于未捕获的异常终止应用程序' NSInternalInconsistencyException',原因:'无法使用标识符FeaturedTableViewCell对单元格进行出列 - 必须为标识符注册nib或类或连接原型故事板中的单元格'

以下是我的表现:

@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

3 个答案:

答案 0 :(得分:0)

我认为你可以通过流程帖子查看此堆栈:POST

ķ。

答案 1 :(得分:0)

确定,

在viewDidLoad上试用

  

YourCustomCell * yourCustomCell = [UINib   nibWithNibName:@“YourCustomCell”bundle:nil]; [self.tableView   registerNib:cellNib forCellReuseIdentifier:@“cell”];

注意:您的自定义单元格当然必须有.xib。

答案 2 :(得分:0)

你可以试试这个,我记得碰到这个确切的问题,这是我试过的解决方案之一: [self.tableView registerClass:[DiscoverTableViewCell class] forCellReuseIdentifier:@"DiscoverTableViewCell"];

然而,我清楚地记得这不起作用。原来我没有正确连接故事板中的单元格。此外,请确保故事板单元格的类已更改为表示单元格的自定义类。