UISearchDisplayController—why does my search result view contain empty cells?

限于喜欢 提交于 2020-01-31 05:32:29

问题


I am about to go out of my mind here, in my Core Data databse I have a lot of users, i have hooked the database up to a tableviewcontroller via NSFetchedResultController, and when the view loads, I see all my users, and i can perform a push to a detail viewcontroller via storyboard segue... so far so god, my tableview contains a custom cell with an image view and 2 uitextlabels that all has their tag values assigned..

Now, when I perform the search I create a new NSFetchedResultController with a predicate, and performs the performFetch.. I then get the fetchedObjects property and it contains the users that match, so the search request is also working like a charm.. in all my tableviews datasources I check if self.tableView == self.searchdispl.view to see which controller I should query data from.. In all the controllers delegates I check to see which controller is active so i know which view to reload. If i nslog a lot of data it is all fine behind the scenes, the delegate and datasource methods all use the correct view and fetchcontroller..

In my cellForRowAtIndexPath i can log out my user.name, and that is always correct, also when searching.

The issue is that the UISearchDisplayController view that is on top only has empty cells, unless i set my UITableViewControllers dynamic prototype cell to basic, then both tableviews contain that label with the users name ! The segue from the cell still doesn't work, but there is data in the cell.

It seems as the UISearchDisplayControllers view is a generic view, at least when it comes to segues and cell layout...

I have checked all the connections from the UISearchDisplayController to my UITableViewController, and all looks fine...

Why will the SearchDisplayControllers view not accept my cell layout and segue ?

Thank you :)

Update : has just figured out what is going wrong, but not found the solution yet. In my cellForRowAtIndexPath the cell is always configured as the Default Cell when thr searchdisplayview is in this method.. Do i really need to create my cell in code for the uisearchdisplay to work ? Why can it not use the configured cell from the storyboard ?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:   (NSIndexPath *)indexPath
{
 static NSString *CellIdentifier = @"PersonCell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    NSLog(@"Default Cell");
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

NSFetchedResultsController *controller = tableView == self.tableView ? self.fetchedResultsController : self.fetchedResultsControllerForSearch;

Person *p;
p = [controller objectAtIndexPath:indexPath];       

[(UILabel*) [cell viewWithTag:1] setText:p.name]; 
[(UILabel*) [cell viewWithTag:2] setText:@"Status"];    

if(p.avatarImage){
    UIImage *avatar = [[UIImage alloc] initWithData:p.avatarImage];
    [(UIImageView *) [cell viewWithTag:3] setImage:avatar];      
}

return cell;

}


回答1:


The issue was that when i got the cell in cellforrowatindexpath, it didn't get it from the original viewcontrollers tableview, so i changed the line from

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

to

UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];

and now all is good, the cell has the layout from storyboard and segues works like a charm..



来源:https://stackoverflow.com/questions/9497860/uisearchdisplaycontroller-why-does-my-search-result-view-contain-empty-cells

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!