UISearchDisplayController not correctly displaying custom cells

后端 未结 6 1504
抹茶落季
抹茶落季 2020-12-29 11:58

So I have a tableView that has sections and rows, and it uses a custom cell class. The custom cell has an image view and a few labels. The table view works fine, and the sea

相关标签:
6条回答
  • 2020-12-29 12:47

    My summary for UITableView with Search Bar and Search Display using same custom cell designed in storyboard protype:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"CellIdentifierAsYouDefinedInStoryboard";
    
        CustomTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
        if (cell == nil) {
            cell = [[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }
    
        if (tableView == self.searchDisplayController.searchResultsTableView) {
            /* searchResultsTableView code here */
        } else {
            /* Base tableView table code here */
        }
    
        /* more cell code here */
    
        return cell;
    }
    


    and then add this line for searchResultsTableView to match your custom cell height:

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        [self.searchDisplayController.searchResultsTableView setRowHeight:self.tableView.rowHeight];
    
         /* more of your viewDidLoad code */
    }
    
    0 讨论(0)
  • 2020-12-29 12:48

    If you are using a UITableView in a UIViewController and you want to reuse a Cell Identifier you created in your StoryBoard for your searchDisplayController, try this:

    StoryBoard > UIViewController > Reference Outlets > link tableView to your UIViewController's .h file and call it something like tableView so you should have something like this:

    @property (strong, nonatomic) IBOutlet UITableView *tableView;
    

    So rather than doing it like this:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]
    }
    

    do this

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]
    }
    
    0 讨论(0)
  • 2020-12-29 12:53

    I got the same exception when I tried to search and some how this fixes it.

        if (tableView == self.searchDisplayController.searchResultsTableView) {
            cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        }else{
           cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];     
        }
    
    0 讨论(0)
  • 2020-12-29 12:54
    [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 
    

    did not work because table view cells are registered to a specific table view. This will not work for your search results controller table view. You did find this out yourself and switched to:

    [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    

    which is the right thing to do.

    Also, designing your custom cell in storyboard will not really work for your search results controller because you are not able to design cells for search table view, only for the main table view.

    Yes, you can register that class for your search table view, as you did here,

    [self.searchDisplayController.searchResultsTableView registerClass:[JSBookCell class] forCellReuseIdentifier:CellIdentifier];
    

    but that will not have any of the stuff you designed in your custom cell in storyboard. You would have to create all programmatically.

    0 讨论(0)
  • 2020-12-29 12:58

    If you create a cell in the storyboard you should not register the class (this actually screws things up). You register the class if you make the cell in code , and you register a nib if you make the cell in the nib. If you make it in the storyboard, you don't register anything, you use dequeueReusableCellWithIdentifier:forIndexPath:, and you don't need the if (cell == nil) clause at all.

    0 讨论(0)
  • 2020-12-29 12:58

    Try with this, it's work for me

    JSBookCell * cell = [yourtableview dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    
    0 讨论(0)
提交回复
热议问题