سؤال

I implemented a UISearchDisplayController on both sides of a MasterViewController. In the Detail view, it works fine with the first item selected from the MasterView.

However, if I choose another Detail item, the search will not display any object even though NSLog tells me it found all of the expected cells:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *feedTableIdentifier = @"unread";
        Post *thepost=nil;
        if (tableView == self.searchDisplayController.searchResultsTableView) {
            NSLog(@"in a search");
            thepost = [self.filteredFeedArray objectAtIndex:indexPath.row];
        } else {
            NSLog(@"NOT in a search");
            thepost = [self.fetchedResultsController objectAtIndexPath:indexPath];
        }
        NSDate *tmpDate=[NSDate date];

        if (thepost.read) {
            tmpDate=thepost.read;
            feedTableIdentifier = @"read";
        } else if (thepost.date) {
            tmpDate=thepost.date;
            feedTableIdentifier = @"unread";
        }
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:feedTableIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc]
                    initWithStyle:UITableViewCellStyleDefault
                    reuseIdentifier:feedTableIdentifier];
        }
        ((UILabel *)[cell viewWithTag:1]).text = thepost.title;
        NSDateFormatter *df=[[NSDateFormatter alloc] init];
        df.dateFormat = @"EEEE, MMMM d, YYYY";
        ((UILabel *)[cell viewWithTag:2]).text = [df stringFromDate:tmpDate];
        ((UILabel *)[cell viewWithTag:3]).text = [self flattenHTML:thepost.excerpt];
        return cell;
    }

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        if (tableView == self.searchDisplayController.searchResultsTableView) {
            NSLog(@"in a search: %d", [_filteredFeedArray count]);
            return [_filteredFeedArray count];
        } else {
            NSLog(@"NOT in a search: %d", [[[_fetchedResultsController sections] objectAtIndex:section] numberOfObjects]);
            return [[[_fetchedResultsController sections] objectAtIndex:section] numberOfObjects];
        }
    }

Both detail and searchresult Table view have samely named Prototype cells.

Can someone tell me what I am doing wrong here and how I could properly reset my UISearchViewController whenever switching Detail items?

هل كانت مفيدة؟

المحلول

Found it:

It's because my search TableView uses prototype cells. The UISearchViewController of my DetailView had this in its viewDidLoad method:

    [self.detailViewController.searchDisplayController.searchResultsTableView registerNib:[UINib nibWithNibName:@"DetailSearchPrototypeCellRead" bundle:nil] forCellReuseIdentifier:@"read"];
    [self.detailViewController.searchDisplayController.searchResultsTableView registerNib:[UINib nibWithNibName:@"DetailSearchPrototypeCellUnRead" bundle:nil] forCellReuseIdentifier:@"unread"];

It appears that this only works the first time the DetailView is populated. I moved the above in the MasterView's didSelectRow... and it now works everytime.

I thought this answer could help others struggling with similar matters.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top