2010-05-09 50 views
1

我一直在关注一个教程来实现搜索栏。从表中推送不同视图

但是,我似乎无法推送不同的意见。这是我的didsSelectRowAtIndexPath外观。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
NSString *selectedCountry = nil; 

if(searching) 
    selectedCountry = [copyListOfItems objectAtIndex:indexPath.row]; 
else { 

    NSDictionary *dictionary = [listOfItems objectAtIndex:indexPath.section]; 
    NSArray *array = [dictionary objectForKey:@"Countries"]; 
    selectedCountry = [array objectAtIndex:indexPath.row]; 
} 

//Initialize the detail view controller and display it. 
if ([[listOfItems objectAtIndex:indexPath.row] isEqual:@"neon"]){ 
Neon *abo = [[Neon alloc] initWithNibName:@"Neon" bundle:nil]; 
//dvController.selectedCountry = selectedCountry; 
[self.navigationController pushViewController:abo animated:YES]; 
[abo release]; 
} 

} 

这里是debuger消息当我点击霓虹灯: 2010-05-09 08:47:27.516 iTeachU [3821:307] *终止应用程序由于未捕获的异常 'NSRangeException',究其原因: '* - [NSMutableArray objectAtIndex:]:索引60超越范围[0..0]' ***第一次调用时调用堆栈: 抛出'NSException'后终止调用 程序接收信号:“SIGABRT” 。

如果任何人有办法推动基于单元格文本的意见,将不胜感激。由于

编辑整个.M

- (void)viewDidLoad { 
[super viewDidLoad]; 
listOfItems = [[NSMutableArray alloc] init]; 

NSArray *ElementsArray = [NSArray arrayWithObjects:@"Actinium", 
          @"Aluminium", 
          @"Americium", 
          @"Antimony", 
          @"Argon", 
          @"Arsenic", 
          @"Astatine", 
          @"Barium", 
          @"Berkelium", 
          @"Beryllium", 
          @"Bismuth", 
          @"Bohrium", 
          @"Boron", 
          @"Bromine", 
          @"Cadmium", 
          @"Cesium", 
          @"Calcium", 
          @"Californium", 
          @"Carbon", 
          @"Cerium", 
          @"Chlorine", 
          @"Chromium", 
          @"Cobalt", 
          @"Copper", 
          @"Curium", 
          @"Darmstadtium", 
          @"Dubnium", 
          @"Dysprosium", 
          @"Einsteinium", 
          @"Erbium", 
          @"Europium", 
          @"Fermium", 
          @"Fluorine", 
          @"Francium", 
          @"Gadolinium", 
          @"Gallium", 
          @"Germanium", 
          @"Gold", 
          @"Hafnium", 
          @"Hassium", 
          @"Helium", 
          @"Holmium", 
          @"Hydrogen", 
          @"Indium", 
          @"Iodine", 
          @"Iridium", 
          @"Iron", 
          @"Krypton", 
          @"Lanthanum", 
          @"Lawrencium", 
          @"Lead", 
          @"Lithium", 
          @"Lutetium", 
          @"Magnesium", 
          @"Manganese", 
          @"Meitnerium", 
          @"Mendelevium", 
          @"Mercury", 
          @"Molybdenum", 
          @"Neodymium", 
          @"neon", 
          @"Neptunium", 
          @"Nickel", 
          @"Niobium", 
          @"Nitrogen", 
          @"Nobelium", 
          @"Osmium", 
          @"Oxygen", 
          @"Palladium", 
          @"Phosphorus", 
          @"Platinum", 
          @"Plutonium", 
          @"Polonium", 
          @"Potassium ", 
          @"Praseodymium", 
          @"Promethium", 
          @"Protactinium", 
          @"Radium", 
          @"Radon", 
          @"Rhenium", 
          @"Rhodium", 
          @"Roentgenium", 
          @"Rubidium", 
          @"Ruthenium", 
          @"Rutherfordium", 
          @"Samarium", 
          @"Scandium", 
          @"Seaborgium", 
          @"Selenium", 
          @"Silicon", 
          @"Silver", 
          @"Sodium", 
          @"Strontium", 
          @"Sulfur", 
          @"Tantalum", 
          @"Technetium", 
          @"Tellurium", 
          @"Terbium", 
          @"Thallium", 
          @"Thorium", 
          @"Thulium", 
          @"Tin", 
          @"Titanium", 
          @"Tungsten", 
          @"Ununbium", 
          @"Ununhexium", 
          @"Ununoctium", 
          @"Ununpentium", 
          @"Ununquadium", 
          @"Ununseptium", 
          @"Ununtrium", 
          @"Uranium", 
          @"Vanadium", 
          @"Xenon", 
          @"Ytterbium", 
          @"Yttrium", 
          @"Zinc", 
          @"Zirconium", nil]; 
NSDictionary *ElementsDict = [NSDictionary dictionaryWithObject:ElementsArray  forKey:@"Elements"]; 


[listOfItems addObject:ElementsDict]; 

//Initialize the copy array. 
copyListOfItems = [[NSMutableArray alloc] init]; 

//Set the title 
self.navigationItem.title = @"Elements"; 

//Add the search bar 
self.tableView.tableHeaderView = searchBar; 
searchBar.autocorrectionType = UITextAutocorrectionTypeNo; 

searching = NO; 
letUserSelectRow = YES; 
} 

-(IBAction) back:(id)sender{ 
[self.navigationController popViewControllerAnimated:NO]; 
} 
// Override to allow orientations other than the default portrait orientation. 
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
// Return YES for supported orientations 
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); 
} 



#pragma mark - 
#pragma mark Table view data source 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 

if (searching) 
    return 1; 
else 
    return [listOfItems count]; 
} 



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 

if (searching) 
    return [copyListOfItems count]; 
else { 

    //Number of rows it should expect should be based on the section 
    NSDictionary *dictionary = [listOfItems objectAtIndex:section]; 
    NSArray *array = [dictionary objectForKey:@"Elements"]; 
    return [array count]; 
} 
} 
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { 

if(searching) 
    return @"Search Results"; 

if(section == 0) 
    return @"Elements"; 
} 

// Customize the appearance of table view cells. 
- (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]; 
} 

// Configure the cell... 

if(searching) 
    cell.text = [copyListOfItems objectAtIndex:indexPath.row]; 
else { 

    //First get the dictionary object 
    NSDictionary *dictionary = [listOfItems objectAtIndex:indexPath.section]; 
    NSArray *array = [dictionary objectForKey:@"Elements"]; 
    NSString *cellValue = [array objectAtIndex:indexPath.row]; 
    cell.text = cellValue; 
} 

return cell; 
} 

- (NSIndexPath *)tableView :(UITableView *)theTableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

if(letUserSelectRow) 
    return indexPath; 
else 
    return nil; 
} 


- (void) searchBarTextDidBeginEditing:(UISearchBar *)theSearchBar { 

//This method is called again when the user clicks back from teh detail view. 
//So the overlay is displayed on the results, which is something we do not want to happen. 
if(searching) 
    return; 

//Add the overlay view. 
if(ovController == nil) 
    ovController = [[OverlayViewController alloc] initWithNibName:@"OverlayView" bundle:[NSBundle mainBundle]]; 

CGFloat yaxis = self.navigationController.navigationBar.frame.size.height; 
CGFloat width = self.view.frame.size.width; 
CGFloat height = self.view.frame.size.height; 

//Parameters x = origion on x-axis, y = origon on y-axis. 
CGRect frame = CGRectMake(0, yaxis, width, height); 
ovController.view.frame = frame;  
ovController.view.backgroundColor = [UIColor grayColor]; 
ovController.view.alpha = 0.5; 

ovController.rvController = self; 

[self.tableView insertSubview:ovController.view aboveSubview:self.parentViewController.view]; 

searching = YES; 
letUserSelectRow = NO; 
self.tableView.scrollEnabled = NO; 

//Add the done button. 
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] 
              initWithBarButtonSystemItem:UIBarButtonSystemItemDone 
              target:self action:@selector(doneSearching_Clicked:)] autorelease]; 
} 

- (void)searchBar:(UISearchBar *)theSearchBar textDidChange:(NSString *)searchText { 

//Remove all objects first. 
[copyListOfItems removeAllObjects]; 

if([searchText length] > 0) { 

    [ovController.view removeFromSuperview]; 
    searching = YES; 
    letUserSelectRow = YES; 
    self.tableView.scrollEnabled = YES; 
    [self searchTableView]; 
} 
else { 

    [self.tableView insertSubview:ovController.view aboveSubview:self.parentViewController.view]; 

    searching = NO; 
    letUserSelectRow = NO; 
    self.tableView.scrollEnabled = NO; 
} 

[self.tableView reloadData]; 
} 

- (void) searchBarSearchButtonClicked:(UISearchBar *)theSearchBar { 

[self searchTableView]; 
} 

- (void) searchTableView { 

NSString *searchText = searchBar.text; 
NSMutableArray *searchArray = [[NSMutableArray alloc] init]; 

for (NSDictionary *dictionary in listOfItems) 
{ 
    NSArray *array = [dictionary objectForKey:@"Elements"]; 
    [searchArray addObjectsFromArray:array]; 
} 

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

    if (titleResultsRange.length > 0) 
     [copyListOfItems addObject:sTemp]; 
} 

[searchArray release]; 
searchArray = nil; 
} 

- (void) doneSearching_Clicked:(id)sender { 

searchBar.text = @""; 
[searchBar resignFirstResponder]; 

letUserSelectRow = YES; 
searching = NO; 
self.navigationItem.rightBarButtonItem = nil; 
self.tableView.scrollEnabled = YES; 

[ovController.view removeFromSuperview]; 
[ovController release]; 
ovController = nil; 

[self.tableView reloadData]; 
} 

//重写以支持编辑表视图。 - (空)的tableView:(UITableView的*)的tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

if (editingStyle == UITableViewCellEditingStyleDelete) { 
    // Delete the row from the data source 
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES]; 
} 
else if (editingStyle == UITableViewCellEditingStyleInsert) { 
    // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view 
} 

} */

#pragma mark - 
#pragma mark Table view delegate 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
NSString *selectedCountry = nil; 

if(searching) 
    selectedCountry = [copyListOfItems objectAtIndex:indexPath.row]; 
else { 

    NSDictionary *dictionary = [listOfItems objectAtIndex:indexPath.section]; 
    NSArray *array = [dictionary objectForKey:@"Countries"]; 
    selectedCountry = [array objectAtIndex:indexPath.row]; 
} 

//Initialize the detail view controller and display it. 
if ([[listOfItems objectAtIndex:indexPath.row] isEqual:@"neon"]){ 
Neon *abo = [[Neon alloc] initWithNibName:@"Neon" bundle:nil]; 
//dvController.selectedCountry = selectedCountry; 
[self.navigationController pushViewController:abo animated:YES]; 
[abo release]; 
} 

} 


#pragma mark - 
#pragma mark Memory management 

- (void)didReceiveMemoryWarning { 
// Releases the view if it doesn't have a superview. 
[super didReceiveMemoryWarning]; 

// Relinquish ownership any cached data, images, etc that aren't in use. 
} 

- (void)viewDidUnload { 
// Relinquish ownership of anything that can be recreated in viewDidLoad or on demand. 
// For example: self.myOutlet = nil; 
} 


- (void)dealloc { 
[back release]; 
[tableView release]; 
[super dealloc]; 
} 


@end 

回答

0

根据例外情况,您正在调用objectAtIndex:,其值为60,NSMutableArray中没有任何内容(范围0..0)。您需要先解决此问题,然后才能看到推送代码。

您应该能够从调试器中的调用堆栈中发现发生异常时正在访问哪个数组,或者您可以在执行objectAtIndex之前添加NSLog:查看您拥有什么。例如,您有:

if ([[listOfItems objectAtIndex:indexPath.row] isEqual:@"neon"]){ 

如果添加上面的一个在此之前下一行,你将能够看到你的索引试图访问(indexPath.row),有多少你确实有在LISTOFITEMS :

NSLog(@"listOfItems has %d, accessing %d", [listOfItems count], indexPath.row); 
+0

谢谢你的回应。我点击了第三项,得到这个: 2010-05-09 19:43:49.392 iTeachU [4674:307] listOfItems有1,访问3 有没有办法告诉这个推动视图使用第3行或类似的东西那? – Tanner 2010-05-10 00:44:59

+0

你在问错误的问题。你应该问为什么listOfItems中只有一个项目?当那里只有1个时,你怎么能期望得到物品3? – progrmr 2010-05-11 13:25:20

0

不知道很多关于你的代码,但我无法告诉您确切的问题,但是:

在顶部,根据searching的值区分两个不同的数组。后来,只有在searching为假时才使用该数组,其索引相同,似乎是NSDictionary s的数组,而不是NSString s。您似乎也第一次使用indexPath.section,第二次使用indexPath.row

我说的是,该行特别是这样一个:

if ([[listOfItems objectAtIndex:indexPath.row] isEqual:@"neon"]){ 

基于前面的代码,也就是这条线:

NSDictionary *dictionary = [listOfItems objectAtIndex:indexPath.section]; 

我猜listOfItems包含的项目数量等于表格视图中的部分数量,而不是单击的部分中的行数,而且我也猜测返回值永远不会等于@"neon",因为它是NSDictionary

+0

伊夫添加的类的其余部分作为编辑。我仍然无法推动。使用提供的代码,您可以看到任何会获得新视图的东西吗? – Tanner 2010-05-09 15:45:33

+0

如果唯一的条目使用键“元素”,您希望这个“国家”数组来自哪里? – 2010-05-10 14:29:58

+0

已将其更改为Elements。选择或推动没有发生变化。 – Tanner 2010-05-10 19:37:35

0

解决这个代码为任何人想知道。 (无效)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {NSString * selectedCountry = nil;

if(searching) 
    selectedCountry = [copyListOfItems objectAtIndex:indexPath.row]; 
else { 

    NSDictionary *dictionary = [listOfItems objectAtIndex:indexPath.section]; 
    NSArray *array = [dictionary objectForKey:@"Elements"]; 
    selectedCountry = [array objectAtIndex:indexPath.row]; 
} 

//Initialize the detail view controller and display it. 
NSLog(@"listOfItems has %d, accessing %d", [listOfItems count], indexPath.row); 
NSDictionary *dictionary = [listOfItems objectAtIndex:indexPath.section]; 
NSArray *array = [dictionary objectForKey:@"Elements"]; 
NSString *cellValue = [array objectAtIndex:indexPath.row]; 

if ([cellValue isEqual:@"neon"]){ 
    NSLog(@"Neon"); 
    Neon *abo = [[Neon alloc] initWithNibName:@"Neon" bundle:nil]; 
    [self.navigationController pushViewController:abo animated:YES]; 
    NSLog(@"Neon Worked"); 
    [abo release]; 
} 

}

+0

如果这些答案中的任何一个能够解决您的问题或帮助您朝正确的方向发展,那么选择一个答案或upvote(如果他们有帮助)会很好。 – progrmr 2010-05-11 14:21:30

+0

这个问题是从它推动那个视图的意义上解决的。但是,搜索栏使用视图的副本。我可以让copyListItems和listItems只推送一个视图。选择其他人会迫使应用程序崩溃。我仍然需要一种方法来推动所有视图。 – Tanner 2010-05-11 20:46:58

+0

通过声明一个字符串并设置isEqual适用于所有包括搜索。你的nslog代码有帮助。我会将它标记为答案,因为它会使我走向正确的道路。感谢您的帮助。 – Tanner 2010-05-11 21:22:51