我是新手,在我的應用程序中,我使用UISearchBar
。當我輸入任何單詞進行搜索時,應用程序崩潰。iPhone:應用程序崩潰當在TableView中使用搜索欄
在我的項目中,我使用的是sqlite,並且從AppDelegate
的db中獲取值,並將其保存在名爲docArray
的NSMutableArray
中。
RootViewController.m
- (void)viewDidLoad{
listOfItems = [[NSMutableArray alloc] init];
NSDictionary *value = [NSDictionary dictionaryWithObject:appDelegate.docArray forKey:@"doctors"];
[listOfItems addObject:value];
// Intialize copy array that will store the result of search result
copyListOfItems = [[NSMutableArray alloc] init];
// Add Search bar to main view .....
self.tableView.tableHeaderView = searchBar;
searchBar.autocorrectionType = UITextAutocorrectionTypeNo;
searching = NO;
letUserSelectRow = YES;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
if (searching)
return 1;
else
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (searching)
return [copyListOfItems count];
else
return [appDelegate.arryData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:CellIdentifier] autorelease];
}
if (searching)
{
waitsup *wu = [copyListOfItems objectAtIndex:indexPath.row];
[cell.textLabel setText:[NSString stringWithFormat:@"%@ %@ %@", wu.first_name, wu.last_name, wu.title]];
cell.imageView.image = [UIImage imageNamed:@"wudoc.jpg"];
[cell.detailTextLabel setText:[NSString stringWithFormat:@"%@ %@ %@", wu.city, wu.state, wu.zipcode]];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
else
{
waitsup *wu = [appDelegate.arryData objectAtIndex:indexPath.row];
[cell.textLabel setText:[NSString stringWithFormat:@"%@ %@ %@", wu.first_name, wu.last_name, wu.title]];
cell.imageView.image = [UIImage imageNamed:@"wudoc.jpg"];
[cell.detailTextLabel setText:[NSString stringWithFormat:@"%@ %@ %@", wu.city, wu.state, wu.zipcode]];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
return cell;
}
-(void)searchBarTextDidBeginEditing :(UISearchBar *)theSearchBar {
NSLog(@"Now in searchBarTextDidBeginEditing");
searching = YES;
letUserSelectRow = NO;
self.tableView.scrollEnabled = NO;
// Add Done button ........
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:self action:@selector(doneSearching_clicked:)]
autorelease];
}
-(NSIndexPath *)tableView :(UITableView *)theTableView willSelectRowAtIndexPath :(NSIndexPath*)indexPath {
NSLog(@"will select Row At Index Path");
if (letUserSelectRow)
{
return indexPath;
}
else
{
return nil;
}
}
-(void)searchBar :(UISearchBar*)theSearchBar textDidChange :(NSString*)searchText {
NSLog(@"textDidChange");
// it is done so that data can be selected as new search
[copyListOfItems removeAllObjects];
if ([searchText length] > 0)
{
searching = YES;
letUserSelectRow = YES;
self.tableView.scrollEnabled = YES;
[self searchTableView];
}
else
{
searching = NO;
letUserSelectRow = NO;
self.tableView.scrollEnabled = NO;
}
[self.tableView reloadData];
}
-(void)searchBarSearchButtonClicked :(UISearchBar*)theSearchBar {
NSLog(@"searchBarSearchButtonClicked");
[self searchTableView];
}
-(void)searchTableView {
NSLog(@"searchTableView");
NSString *searchText = searchBar.text;
NSMutableArray *searchArray = [[NSMutableArray alloc] init];
for (NSDictionary *dictionary in listOfItems)
{
NSArray *array = [dictionary objectForKey:@"first_name"];
[searchArray addObjectsFromArray:array];
}
for (waitsup *sTemp in searchArray)
{
NSRange titleResultsRange = [[NSString stringWithFormat:@"%@ %@",sTemp.first_name, sTemp.last_name] rangeOfString:searchText options:NSCaseInsensitiveSearch];
if (titleResultsRange.length > 0)
{
NSLog(@"%@", sTemp);
[copyListOfItems addObject:sTemp];
}
}
[searchArray release];
searchArray = nil;
}
-(void)doneSearching_clicked:(id)sender {
NSLog(@"doneSearching_clicked");
searchBar.text = @" ";
[searchBar resignFirstResponder];
letUserSelectRow = YES;
searching = NO;
self.navigationItem.rightBarButtonItem = nil;
self.tableView.scrollEnabled = YES;
[self.tableView reloadData];
}
控制檯顯示以下錯誤:
2010-07-22 13:36:16.002 wu2[1077:207] Processing Element: array
2010-07-22 13:36:16.004 wu2[1077:207] Processing Element: XML_Serializer_Tag
2010-07-22 13:36:16.005 wu2[1077:207] Processing Element: id
2010-07-22 13:36:16.005 wu2[1077:207] Processing Value: 1
2010-07-22 13:36:16.006 wu2[1077:207] Processing Element: first_name
2010-07-22 13:36:16.007 wu2[1077:207] Processing Value: Prateek
2010-07-22 13:36:16.007 wu2[1077:207] Processing Element: middle_name
2010-07-22 13:36:16.008 wu2[1077:207] Processing Value: K
2010-07-22 13:36:16.008 wu2[1077:207] Processing Element: last_name
2010-07-22 13:36:16.009 wu2[1077:207] Processing Value: Bhanot
2010-07-22 13:36:16.009 wu2[1077:207] Processing Element: title
2010-07-22 13:36:16.010 wu2[1077:207] Processing Value: Er.
2010-07-22 13:36:16.011 wu2[1077:207] Processing Element: org_name
2010-07-22 13:36:16.011 wu2[1077:207] Processing Value: Cyber
2010-07-22 13:36:16.012 wu2[1077:207] Processing Element: upin
2010-07-22 13:36:16.014 wu2[1077:207] Processing Value: 34242
2010-07-22 13:36:16.014 wu2[1077:207] Processing Element: npi
2010-07-22 13:36:16.015 wu2[1077:207] Processing Value: 2343242
2010-07-22 13:36:16.015 wu2[1077:207] Processing Element: address1
2010-07-22 13:36:16.015 wu2[1077:207] Processing Value: Maler Kotla
2010-07-22 13:36:16.016 wu2[1077:207] Processing Element: address2
2010-07-22 13:36:16.016 wu2[1077:207] Processing Value: Mohali
2010-07-22 13:36:16.018 wu2[1077:207] Processing Element: city
2010-07-22 13:36:16.018 wu2[1077:207] Processing Value: Chandigarh
2010-07-22 13:36:16.018 wu2[1077:207] Processing Element: state
2010-07-22 13:36:16.019 wu2[1077:207] Processing Value: Punja
2010-07-22 13:36:16.020 wu2[1077:207] Processing Element: zipcode
2010-07-22 13:36:16.020 wu2[1077:207] Processing Value: 12345
2010-07-22 13:36:16.021 wu2[1077:207] Processing Element: country
2010-07-22 13:36:16.022 wu2[1077:207] Processing Value: India
2010-07-22 13:36:16.022 wu2[1077:207] Processing Element: county
2010-07-22 13:36:16.023 wu2[1077:207] Processing Element: office_phone1
2010-07-22 13:36:16.025 wu2[1077:207] Processing Value: 13123131
2010-07-22 13:36:16.027 wu2[1077:207] Processing Element: fax1
2010-07-22 13:36:16.028 wu2[1077:207] Processing Value: 2131231
2010-07-22 13:36:16.028 wu2[1077:207] Processing Element: gender
2010-07-22 13:36:16.029 wu2[1077:207] Processing Value: male
2010-07-22 13:36:16.030 wu2[1077:207] Processing Element: status
2010-07-22 13:36:16.030 wu2[1077:207] Processing Value: active
2010-07-22 13:36:16.031 wu2[1077:207] Processing Element: profiletype
2010-07-22 13:36:16.031 wu2[1077:207] Processing Value: user
2010-07-22 13:36:16.032 wu2[1077:207] Processing Element: entity
2010-07-22 13:36:16.033 wu2[1077:207] Processing Value: practice
2010-07-22 13:36:16.034 wu2[1077:207] Processing Element: XML_Serializer_Tag
2010-07-22 13:36:16.035 wu2[1077:207] Processing Element: id
2010-07-22 13:36:16.035 wu2[1077:207] Processing Value: 2
2010-07-22 13:36:16.036 wu2[1077:207] Processing Element: first_name
2010-07-22 13:36:16.036 wu2[1077:207] Processing Value: Hitu
2010-07-22 13:36:16.037 wu2[1077:207] Processing Element: middle_name
2010-07-22 13:36:16.038 wu2[1077:207] Processing Value: k
2010-07-22 13:36:16.038 wu2[1077:207] Processing Element: last_name
2010-07-22 13:36:16.039 wu2[1077:207] Processing Value: bansal
2010-07-22 13:36:16.040 wu2[1077:207] Processing Element: title
2010-07-22 13:36:16.040 wu2[1077:207] Processing Value: Doctor
2010-07-22 13:36:16.041 wu2[1077:207] Processing Element: org_name
2010-07-22 13:36:16.041 wu2[1077:207] Processing Value: Cyebertron
2010-07-22 13:36:16.042 wu2[1077:207] Processing Element: upin
2010-07-22 13:36:16.042 wu2[1077:207] Processing Value: 34242
2010-07-22 13:36:16.043 wu2[1077:207] Processing Element: npi
2010-07-22 13:36:16.043 wu2[1077:207] Processing Value: 2343242
2010-07-22 13:36:16.044 wu2[1077:207] Processing Element: address1
2010-07-22 13:36:16.045 wu2[1077:207] Processing Value: Mohali
2010-07-22 13:36:16.045 wu2[1077:207] Processing Element: address2
2010-07-22 13:36:16.046 wu2[1077:207] Processing Value: chd
2010-07-22 13:36:16.046 wu2[1077:207] Processing Element: city
2010-07-22 13:36:16.047 wu2[1077:207] Processing Value: Chandigarh
2010-07-22 13:36:16.048 wu2[1077:207] Processing Element: state
2010-07-22 13:36:16.048 wu2[1077:207] Processing Value: Punja
2010-07-22 13:36:16.049 wu2[1077:207] Processing Element: zipcode
2010-07-22 13:36:16.049 wu2[1077:207] Processing Value: 12345
2010-07-22 13:36:16.050 wu2[1077:207] Processing Element: country
2010-07-22 13:36:16.050 wu2[1077:207] Processing Value: India
2010-07-22 13:36:16.051 wu2[1077:207] Processing Element: county
2010-07-22 13:36:16.051 wu2[1077:207] Processing Value: jljljlkj
2010-07-22 13:36:16.052 wu2[1077:207] Processing Element: office_phone1
2010-07-22 13:36:16.053 wu2[1077:207] Processing Value: 12-32-3-3
2010-07-22 13:36:16.053 wu2[1077:207] Processing Element: fax1
2010-07-22 13:36:16.054 wu2[1077:207] Processing Value: 12331331
2010-07-22 13:36:16.054 wu2[1077:207] Processing Element: gender
2010-07-22 13:36:16.055 wu2[1077:207] Processing Value: male
2010-07-22 13:36:16.056 wu2[1077:207] Processing Element: status
2010-07-22 13:36:16.056 wu2[1077:207] Processing Value: active
2010-07-22 13:36:16.057 wu2[1077:207] Processing Element: profiletype
2010-07-22 13:36:16.057 wu2[1077:207] Processing Value: dr411
2010-07-22 13:36:16.058 wu2[1077:207] Processing Element: entity
2010-07-22 13:36:16.058 wu2[1077:207] Processing Value: provider
2010-07-22 13:36:16.059 wu2[1077:207] No Errors
2010-07-22 13:36:20.923 wu2[1077:207] Now in searchBarTextDidBeginEditing
2010-07-22 13:36:22.899 wu2[1077:207] textDidChange
2010-07-22 13:36:22.900 wu2[1077:207] searchTableView
2010-07-22 13:36:22.901 wu2[1077:207] <waitsup: 0x6e14fc0>
2010-07-22 13:36:22.985 wu2[1077:207] textDidChange
2010-07-22 13:36:22.986 wu2[1077:207] searchTableView
2010-07-22 13:36:22.987 wu2[1077:207] <waitsup: 0x6e14fc0>
2010-07-22 13:36:23.073 wu2[1077:207] textDidChange
2010-07-22 13:36:23.074 wu2[1077:207] searchTableView
2010-07-22 13:36:23.075 wu2[1077:207] <waitsup: 0x6e14fc0>
2010-07-22 13:36:23.249 wu2[1077:207] textDidChange
2010-07-22 13:36:23.250 wu2[1077:207] searchTableView
2010-07-22 13:36:23.250 wu2[1077:207] <waitsup: 0x6e14fc0>
2010-07-22 13:36:23.449 wu2[1077:207] textDidChange
2010-07-22 13:36:23.450 wu2[1077:207] searchTableView
2010-07-22 13:36:23.450 wu2[1077:207] <waitsup: 0x6e14fc0>
2010-07-22 13:36:23.625 wu2[1077:207] textDidChange
2010-07-22 13:36:23.626 wu2[1077:207] searchTableView
2010-07-22 13:36:23.626 wu2[1077:207] <waitsup: 0x6e14fc0>
2010-07-22 13:36:23.777 wu2[1077:207] textDidChange
2010-07-22 13:36:23.778 wu2[1077:207] searchTableView
2010-07-22 13:36:23.778 wu2[1077:207] <waitsup: 0x6e14fc0>
2010-07-22 13:36:24.377 wu2[1077:207] searchBarSearchButtonClicked
2010-07-22 13:36:24.377 wu2[1077:207] searchTableView
2010-07-22 13:36:24.378 wu2[1077:207] <waitsup: 0x6e14fc0>
2010-07-22 13:36:26.157 wu2[1077:207] doneSearching_clicked
2010-07-22 13:36:26.161 wu2[1077:207] textDidChange
2010-07-22 13:36:26.161 wu2[1077:207] searchTableView
2010-07-22 13:36:26.162 wu2[1077:207] <waitsup: 0x6e14fc0>
2010-07-22 13:36:26.162 wu2[1077:207] <waitsup: 0x6e16100>
2010-07-22 13:36:27.071 wu2[1077:207] Now in searchBarTextDidBeginEditing
2010-07-22 13:36:28.329 wu2[1077:207] textDidChange
2010-07-22 13:36:28.330 wu2[1077:207] searchTableView
2010-07-22 13:36:30.221 wu2[1077:207] doneSearching_clicked
2010-07-22 13:36:30.224 wu2[1077:207] textDidChange
2010-07-22 13:36:30.224 wu2[1077:207] searchTableView
2010-07-22 13:36:30.225 wu2[1077:207] <waitsup: 0x6e14fc0>
2010-07-22 13:36:30.226 wu2[1077:207] <waitsup: 0x6e16100>
感謝您的更新信息,請參閱下面我建議的修復程序。從中學到的一件事是,核心錯誤是這樣的:' - [waitsup rangeOfString:options:]:無法識別的選擇器發送到實例0x6d0f8e0',因爲下面列出的原因。 – mvds 2010-07-16 22:15:14