开发者

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

开发者 https://www.devze.com 2023-03-26 09:00 出处:网络
today I created a tableView, similar to the iPhone Contacts app. I placed a TableView into the first cell, using the code

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 = @"SearchCel开发者_如何转开发l";
        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 :)


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]; 


Pls add UISearchBarDelegate like this in .h

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

and in .m put code

searchBar.delegate=self;


Rather than coding this much you can directly add searchbar in tableview from xib i.e. 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消