Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8823757
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T06:26:17+00:00 2026-06-14T06:26:17+00:00

I try to create a simple app for iPhone. In it i have a

  • 0

I try to create a simple app for iPhone.

In it i have a tableview and a detailview of the tableview, and last a searchbar to filter the tableview.

The tableview and detailview works fine, but the searchhbar dos not work as it should.

When i type in a search word it displays the whole tableview and the detailview dos not work
search mode.

When i cancel the search mode the detail view works again.

So obviusly my filter code is wrong, and i haven’t got my detailview inside my filter code but i don’t now how to do that correctly.

Hope someone can help me, my code look like this:

my *.h file:

#import <UIKit/UIKit.h>

@class DetailViewController;

@interface MasterViewController : UITableViewController <UISearchBarDelegate, UISearchDisplayDelegate>

{
    NSArray *originalData;
    NSMutableArray *searchData;

    UISearchBar *searchBar;
    UISearchDisplayController *searchDisplayController;

}

@property (strong, nonatomic) DetailViewController *detailViewController;

@end

my *.m file:

#import "MasterViewController.h"

#import "DetailViewController.h"

@interface MasterViewController () {
    NSMutableArray *_objects;
}
@end

@implementation MasterViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = NSLocalizedString(@"Opslag", @"Opslag");
        _objects = [[NSMutableArray alloc] init];

        //title
        //detail

        NSDictionary * obj1 = [[NSDictionary alloc] initWithObjectsAndKeys:@"this_title", @"title", @"this_detail", @"detail", nil];
        NSDictionary * obj2 = [[NSDictionary alloc] initWithObjectsAndKeys:@"some_title2", @"title", @"some_detail2", @"detail", nil];
        NSDictionary * obj3 = [[NSDictionary alloc] initWithObjectsAndKeys:@"some_title3", @"title", @"some_detail3", @"detail", nil];
        NSDictionary * obj4 = [[NSDictionary alloc] initWithObjectsAndKeys:@"some_title4", @"title", @"some_detail4", @"detail", nil];

        [_objects addObject:obj1];
        [_objects addObject:obj2];
        [_objects addObject:obj3];
        [_objects addObject:obj4];

        originalData = [[NSArray alloc] initWithObjects:obj1, obj2, obj3, nil];
        searchData = [[NSMutableArray alloc] init];


    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 160, 44)];
    searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];

    searchDisplayController.delegate = self;
    searchDisplayController.searchResultsDataSource = self;

    self.tableView.tableHeaderView = searchBar;

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



#pragma mark - Table View

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _objects.count;
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }

    cell.textLabel.text = [[_objects objectAtIndex:indexPath.row] objectForKey:@"title"];

    cell.detailTextLabel.text = [[_objects objectAtIndex:indexPath.row] objectForKey:@"detail"];




    return cell;
}

#pragma mark - searchDisplayControllerDelegate

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
    [searchData removeAllObjects];

    NSArray *group;

    for(group in originalData)
    {
        NSMutableArray *newGroup = [[NSMutableArray alloc] init];
        NSString *element;

        for(element in group)
        {
            NSRange range = [element rangeOfString:searchString options:NSCaseInsensitiveSearch];

            if (range.length > 0) {
                [newGroup addObject:element];
            }
        }

        if ([newGroup count] > 0) {
            [searchData addObject:newGroup];
        }


    }

    return YES;
}


- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (!self.detailViewController) {
        self.detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];
    }
    NSDictionary *object = _objects[indexPath.row];
    self.detailViewController.detailItem = [object objectForKey:@"detail"];
    self.detailViewController.titleItem = [object objectForKey:@"title"];
    [self.navigationController pushViewController:self.detailViewController animated:YES];
}

@end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T06:26:18+00:00Added an answer on June 14, 2026 at 6:26 am

    your cellForRowAtIndexPath method is always picking from your _objects array, which you never change when a search is performed. When shouldReloadTableForSearchString is called you only populate searchData, which is never fed back into cellForRowAtIndexPath and numberOfRowsInSection to show the filtered results.

    One way to handle that is to do something like this (untested) in your cellForRowAtIndexPath:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"Cell";
    
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        }
    
        NSArray* sourceArray = tableView == self.searchDisplayController.searchResultsTableView ? searchData : _objects;
    
        cell.textLabel.text = [[sourceArray objectAtIndex:indexPath.row] objectForKey:@"title"];
    
        cell.detailTextLabel.text = [[sourceArray objectAtIndex:indexPath.row] objectForKey:@"detail"];
    
        return cell;
    }
    

    and also in your numberOfRowsInSection

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        if (tableView == self.searchDisplayController.searchResultsTableView){
            return searchData.count;
        }else
        {
           return _objects.count;
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a create a simple app where I have a TableView
I try to build a simple iPhone app. I want to create a custom
I try to create a simple heroku app which clones a git repository, invokes
I try to create a very simple app using windows API. I've done some
I try to create styles with first-child and last-child items but I encountered a
Hi have a problem with singleton Class in iPhone app. I have create a
I am trying to create a simple 3-D app for android that will have
in my Silverlight 4 app, I try to create a simple UserControl, which will
I'm trying to create a simple app that looks up a Place, but I'm
I'm new to Java and Android development and try to create a simple app

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.