質問

I am pretty new in objective c so hopefully this all make sense.I have followed this video tutorial from YouTube.The program is not working . You can download the project from this link.Guide me to make this program functional.

役に立ちましたか?

解決 2

in your view controller.h file

import

@interface RecipeBookViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> 
{

  NSArray *recipes;   //this is used for load the data to table
   NSArray *searchResults; //this is used for search the particular name in uitableview
}

@property (nonatomic, strong) UITableView *tableView;    //progrmatically allocationg the tableview

.m file

@synthesize tableView;  //this is used accessing the name in more than one time


- (void)viewDidLoad
{
[super viewDidLoad];
// Initialize table data

searchResults=[NSArray alloc]init];

recipes = [NSArray arrayWithObjects:@"Egg Benedict", @"Mushroom Risotto", @"Full Breakfast", @"Hamburger", @"Green Tea", @"Thai Shrimp Cake", @"Angry Birds Cake", @"Ham and Cheese Panini", nil];

//create the tableview
tableView =[[UITableView alloc]initWithFrame:CGRectMake(0, 163, 320, 317)];
tableView.delegate=self;
tableView.dataSource=self;
[self. tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];

[self.view addSubview: tableView];

}


//this is for loading the table data

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView == self.searchDisplayController.searchResultsTableView) {
    return [searchResults count];

} else {
    return [recipes count];

}
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}

if (tableView == self.searchDisplayController.searchResultsTableView) {
    cell.textLabel.text = [searchResults objectAtIndex:indexPath.row];
} else {
    cell.textLabel.text = [recipes objectAtIndex:indexPath.row];
}

return cell;
}

//this is for search bar delegate method for filtering

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
NSPredicate *resultPredicate = [NSPredicate 
                                predicateWithFormat:@"SELF contains[cd] %@",
                                searchText];

searchResults = [recipes filteredArrayUsingPredicate:resultPredicate];
}

#pragma mark - UISearchDisplayController delegate methods
 -(BOOL)searchDisplayController:(UISearchDisplayController *)controller 
 shouldReloadTableForSearchString:(NSString *)searchString
 {
 [self filterContentForSearchText:searchString 
                           scope:[[self.searchDisplayController.searchBar scopeButtonTitles]
                                  objectAtIndex:   [self.searchDisplayController.searchBar
                                                 selectedScopeButtonIndex]]];

return YES;
}

finally add the one search bar display controller only in top on the view

他のヒント

use the following tutorial is the simple way to get the answer and and also u get the sample code in github, the link is http://www.appcoda.com/how-to-add-search-bar-uitableview/

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar;                     // called when text starts editing
{
    if (searching)
    {
        searching=YES;
        letsUserSelectRow=YES;
    }
    else
    {
        searching=NO;
        letsUserSelectRow=YES;
    }
    // self.tbl_searchtable.scrollEnabled=NO;
}

- (void)searchBarCancelButtonClicked:(UISearchBar *) searchBar
{
    searching = NO;
    letsUserSelectRow = YES;
    searchBar.text = @"";
    [searchBar resignFirstResponder];
    [self.tbl_searchuser reloadData];
}

-(NSIndexPath *)tableView :(UITableView *)theTableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(letsUserSelectRow)
        return indexPath;
    else
        return nil;
}

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText;   // called when text changes (including clear)
{
    _tbl_searchuser.hidden=FALSE;

    [copylistofitem removeAllObjects];

    if([searchText length] > 0)
    {
        searching = YES;
        letsUserSelectRow = YES;
        self.tbl_searchuser.scrollEnabled = YES;
        [self searchtableview];
    }
    else
    {
        searching = NO;
        letsUserSelectRow= NO;
        [_searchbar resignFirstResponder];
        //self.tbl_searchtable.scrollEnabled = NO;
    }

    [copylistofitem retain];
    [self.tbl_searchuser reloadData];
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar;                     // called when keyboard search button pressed
{
    [self hide];
    //[self searchtableview];
}

-(void)searchtableview
{
    _tbl_searchuser.frame=CGRectMake(0, 65, 320, 140);
    NSString *searchText = _searchbar.text;
    NSLog(@"%@",array_allusers);
    for(int l = 0; l < [array_allusers count]; l++)
    {
        NSString *tempstr = [[array_allusers objectAtIndex:l] objectForKey:@"username"];
        NSRange rngstr = [tempstr rangeOfString:searchText options:(NSAnchoredSearch | NSCaseInsensitiveSearch)];

        if(rngstr.length > 0)
        {
            [copylistofitem addObject:[array_allusers objectAtIndex:l]];
            [copylistofitem retain];
        }
    }
    NSLog(@"%@",copylistofitem);

    NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"username" ascending:YES];
    [copylistofitem sortUsingDescriptors:[NSArray arrayWithObject:sort]];

    [_tbl_searchuser reloadData];










}
-(void)hide
{
    [_searchbar resignFirstResponder];
    _tbl_searchuser.frame=CGRectMake(0, 65, 320, 274);

}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (searching && [_searchbar.text length] > 0)
        return [copylistofitem count];
    else
        return yourarray;

}

NSMutableArray *copylistofitem;
BOOL searching;
BOOL letsUserSelectRow;
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top