简体   繁体   中英

Help with UISearchBar, located in first cell of UITableView? Won't enter searchBar:textDidChange method

today I created a tableView, similar to the iPhone Contacts app. I placed a TableView into the first cell, using the code

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([indexPath indexAtPosition:0] == 0) {
        static NSString *CellIdentifier = @"SearchCell";
        UITableViewCell *searchBarCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        searchBar = [[UISearchBar alloc] initWithFrame:searchBarCell.frame];
        [searchBarCell addSubview:searchBar];
        return searchBarCell;
    } // ...

The searchBar displays correctly, but when I implemented the search methods, I found that they are not being entered when I type into the searchBar... for example this method:

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchTerm

I think it is because the searchBar is a subview of another class, the tableView Cell? Thats why the search bar can't access the search methods? I also set the search bar delegate to self, still nothing.

Can any body help? Thanks a lot in advance :)

Pls add UISearchBarDelegate like this in .h

@interface CategoriesViewController : UIViewController <UISearchBarDelegate,UITableViewDelegate,UITableViewDataSource>{

and in .m put code

searchBar.delegate=self;

You are not setting the delegate of your UISearchBar. I read in a comment you did it in other part, but you have to do it after you create the object. I mean after the line:

searchBar = [[UISearchBar alloc] initWithFrame:searchBarCell.frame]; 

Rather than coding this much you can directly add searchbar in tableview from xib ie subview of table. else you can take uisearchbar as searchBar in .h file and you have to assign this searchBar and than delegate searchbar and use all the methods of searchbar as taken in .h file.

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