简体   繁体   中英

Implementing a search bar iOS 6 table view static cell

Im trying to implement a search bar on a Static Cell table view.

I have subclassed the UITableview controller, declared the Search Bar and Search Display Controller in .h and also set <UISearchDisplayDelegate>

Now im unsure what to do next in my .m file, the tutorials ive looked at all refer to prototype cells and I cant seem to adapt the code for my requirements. I have also looked at Apples sample code

My end result is to search the cells on the page from the search bar. My search bar brings up the rows but does not sort them according the the search. Further to that, when returning to the table view it seems corrupted graphically.

Using Xcode 4.5

There is a lot being asked in that question. Here are some tips:

Add the search bar to your table header

self.tableView.tableHeaderView = self.searchBar;

Hide this by default, when you scroll down it will appear

[self.tableView setContentOffset:CGPointMake(0, 44)];

As for searching / filtering the table. What you need is the table datasource methods pointing at an array of data. You also need to keep another array of the full list of data. On searching, call a method like this:

    - (void) performSearchWithText:(NSString *)searchText {
       if ([searchText length] > 0) {
            NSPredicate *predicate = [NSPredicate predicateWithFormat:@"cell contains[cd]   %@", searchText]; 
            NSArray *filteredArray = [self.allData filteredArrayUsingPredicate:predicate];
            self.tableData = [NSArray arrayWithArray:filteredArray];
            [self.tableView reloadData];
        } else {
            self.tableData = self.allData;
            [self.tableView reloadData];   
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM