Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UISearchbar with UIStoryboard: segues and custom cells do not work correctly

I am using storyboards and want to implement a UISearchbar for my UITableView. The UISearchbarController generates a new UITableView and I use the following strategy:

if (tableView == self.tableView)
   //Populate table view with normal data
else
   //Populate table view with search data

The first problem is to deal with custom cells. I have to instantiate them in cellForRowAtIndexPath. Normally you would do that from a nib file. How do I do this from with storyboards? dequeueReusableCellWithIdentifier returns nil.

The second problem concerns the segue from the table view to the detail view. The segue is initiated in the normal table view, but not in the search table view. Since Storyboards hides everything I have no idea how to assign segues to cells of the search table view.

Could anyone help, please? Thanks!

like image 223
Johannes Avatar asked Jan 30 '26 13:01

Johannes


2 Answers

Regarding the first problem i solved it by changing the ViewController type from UITableViewController to UIViewController , then adding table view inside it and add IBOutlet for the table view

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

use the outlet to dequeue the table cell

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MyTableViewCell *cell=[_tableView dequeueReusableCellWithIdentifier:productMasterCellIdentifier];
    // .. modify your cell
    return cell;
}
like image 161
Mahmoud Adam Avatar answered Feb 01 '26 05:02

Mahmoud Adam


About the second problem: don't link the segue from the cell, but from the viewcontroller. You will need to make two segues in your storyboard with different names. Then manually invoke the segue when the row is selected:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([tableView isEqual:self.searchDisplayController.searchResultsTableView]) {
        [self performSegueWithIdentifier:@"fromSearchResults" sender:self];
    }
    else {
        [self performSegueWithIdentifier:@"fromAll" sender:self];
    }
}  

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"fromSearchResults"]) {
        ..........
        ..........
    }
    else {
        ..........
        ..........
    }
}
like image 41
fotospiro Avatar answered Feb 01 '26 04:02

fotospiro



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!