我一直在关注实施搜索栏的教程。
然而,我似乎无法推出不同的观点。这是我的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];
}
//覆盖以支持编辑表格视图。 - (void)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 :(得分: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
。
答案 1 :(得分: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);
答案 2 :(得分:0)
解决了这个代码,对任何想知道的人。 - (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:@"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];
}
}