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.
精彩评论