Ios4 点击搜索栏使用Objective C在iPhone上终止我的程序

Ios4 点击搜索栏使用Objective C在iPhone上终止我的程序,ios4,Ios4,我想在表视图中添加简单的搜索栏..程序运行时没有错误..但当我尝试在搜索栏中添加文本时,程序会终止..以下是我的代码:- @接口RootViewController:UITableViewController{ NSArray*名单 IBOutlet UISearchBar *searchBar; NSMutableArray *searchresult; BOOL isSearchon; BOOL canSelectRow; } @属性(非原子,保留)NSArray*列表; @属性(

我想在表视图中添加简单的搜索栏..程序运行时没有错误..但当我尝试在搜索栏中添加文本时,程序会终止..以下是我的代码:-

@接口RootViewController:UITableViewController{ NSArray*名单

IBOutlet UISearchBar *searchBar;


NSMutableArray *searchresult;

BOOL isSearchon;

BOOL canSelectRow;
} @属性(非原子,保留)NSArray*列表; @属性(非原子,保留)IBUISearchBar*搜索栏; -(无效)完成搜索:(id)发件人

-(作废)搜索列表

@结束

//英寸

@RootViewController的实现

@综合列表、搜索栏

-(无效)完成搜索:(id)发件人

{ isSearchon=否

canSelectRow=YES;

self.tableView.scrollEnabled=YES;

self.navigationItem.rightBarButtonItem=nil;

[searchBar resignFirstResponder];

[self.tableView reloadData];
}

-(作废)搜索列表

{NSString*searchText=searchBar.text

[searchresult removeAllObjects];



for(NSString *str in list)
{
    NSRange titleResultsRange=[str rangeOfString:searchText options:NSCaseInsensitiveSearch];


    if (titleResultsRange.length > 0)
        [searchresult addObject:str];
}
}

  • (无效)搜索栏搜索按钮选中:(ui搜索栏*)搜索栏{

    [自我搜索列表]; }

  • (无效)搜索栏:(UISearchBar*)搜索栏文本更改:(NSString*)搜索文本{

如果([searchText length]>0)

{isSearchon=是

canSelectRow=YES;

self.tableView.scrollEnabled=YES;

[self searchlist];}
canSelectRow=NO;

self.tableView.scrollEnabled=NO;

self.navigationItem.rightBarButtonItem=[[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneSearching:)]autorelease];
否则{ isSearchon=否

canSelectRow=NO;

self.tableView.scrollEnabled=NO;

}

[self.tableView reloadData];
}

  • (无效)viewDidLoad{ [超级视图下载]

    list=[[NSArray alloc]initWithObjects:@“rohan”,“vidhya”,“kavita”,“pushkar”,nil]

    self.tableView.tableHeaderView=搜索栏

    searchBar.autocorrectionType=UITextAutocorrectionTypeYes

    searchresult=[[NSMutableArray alloc]init]

    isSearchon=否

    canSelectRow=NO;
    
    self.tableView.scrollEnabled=NO;
    
    }
    
    [self.tableView reloadData];
    
    canSelectRow=是

    self.navigationItem.title=@“名称”

}

-(无效)searchBarTextDidBeginEditing:(UISearchBar*)搜索栏

{ isSearchon=是

canSelectRow=YES;

self.tableView.scrollEnabled=YES;

[self searchlist];}
canSelectRow=NO;

self.tableView.scrollEnabled=NO;

self.navigationItem.rightBarButtonItem=[[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneSearching:)]autorelease];
}

  • (NSInteger)表格视图中的节数:(UITableView*)表格视图{

    返回1; }

//自定义表视图中的行数。 -(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节 { 如果(isSearchon){ 返回[搜索结果计数];}

else{

        return [list count];}
}

//自定义表格视图单元格的外观。 -(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{

static NSString *CellIdentifier = @"Cell";


UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}

if(isSearchon)
{
    cell.text=[searchresult objectAtIndex:indexPath.row];}

    else{
        cell.text=[list objectAtIndex:indexPath.row];}

// Configure the cell.

return cell;
}

  • (无效)解除锁定{ [super dealoc]; [搜索栏释放]; }

@结束

我在我的问题中编辑了我的代码…它可以正常工作nw…任何愿意在iPhone tableview中使用searchbar的人都可以参考代码。

我在我的问题中编辑了我的代码…它可以正常工作nw…任何愿意在iPhone tableview中使用searchbar的人都可以参考代码