iOS search bar not showing results

后端 未结 3 706
小鲜肉
小鲜肉 2021-01-23 09:18

*Update: This actually works, the style for my custom cell hasn\'t come across and so the cell looks blank. How then do I get the searchResultsTableView to use my

3条回答
  •  梦谈多话
    2021-01-23 10:02

    If you want to use the same cell for your main table and the search results table, you should make the cell in a xib (or you could do it entirely in code). In viewDidLoad, register the nib for both table views,

    - (void)viewDidLoad {
        [super viewDidLoad];
        [self.searchDisplayController.searchResultsTableView registerNib:[UINib nibWithNibName:@"InviteTableViewCell" bundle:nil] forCellReuseIdentifier:@"InviteCell"];
        [self.tableView registerNib:[UINib nibWithNibName:@"InviteTableViewCell" bundle:nil] forCellReuseIdentifier:@"inviteCell"];
    }
    

    In cellForRowAtIndexPath, you can do something like this,

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        InviteTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"InviteCell" forIndexPath:indexPath];
        MyUser *person = ([tableView isEqual:self.tableView])? self.inviteContactsArray[indexPath.row] : self.filteredContactArray[indexPath row];
        cell.nameLabel.text = person.fullName;
        cell.emailLabel.text = person.email;
        return cell;
    }
    

    If you've already setup your cell in the storyboard, you can copy and paste it into an empty xib file, so you don't have to set it up all over again. You can delete the cell from your storyboard table view since you will be getting the cell from the xib file instead.

提交回复
热议问题