【问题标题】:Unable to dequeue a cell when searching table view parse.com iOS搜索表格视图时无法使单元格出列 parse.com iOS
【发布时间】:2015-04-16 23:34:25
【问题描述】:

我正在尝试在我的表格视图中执行搜索功能,以从我的 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

【问题讨论】:

  • 您没有向我们提供最必要的信息。你的单元是用故事板还是用笔尖构建的?
  • 它是从 Storyboard @PatrickBush 构建的
  • 你可以试试这个,我记得遇到过这个问题,这是我尝试过的解决方案之一:[self.tableView registerClass:[SiteFileCell class] forCellReuseIdentifier:@"FileCell"];

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


【解决方案1】:

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

K.

【讨论】:

    【解决方案2】:

    好的,

    在 viewDidLoad 上试试

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

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

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-11-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-03-07
        • 2018-08-19
        • 2015-09-30
        • 1970-01-01
        相关资源
        最近更新 更多