Iphone 从表中推送不同的视图
我一直在遵循一个教程来实现搜索栏 然而,我似乎无法提出不同的观点。这是我的DidsElectroWatineXpath看起来的样子Iphone 从表中推送不同的视图,iphone,uitableview,didselectrowatindexpath,Iphone,Uitableview,Didselectrowatindexpath,我一直在遵循一个教程来实现搜索栏 然而,我似乎无法提出不同的观点。这是我的DidsElectroWatineXpath看起来的样子 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { NSString *selectedCountry = nil; if(searching) selectedCountry = [copyListOfItems obj
- (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];
}
}
下面是我单击neon时的调试程序消息:
2010-05-09 08:47:27.516 iTeachU[3821:307]*由于未捕获的异常“NSRangeException”而终止应用程序,原因:“*-[NSMutableArray objectAtIndex::]:索引60超出边界[0..0]”
***第一次抛出时调用堆栈:
在抛出“NSException”实例后调用terminate
程序收到信号:“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提交的编辑样式:(UITableViewCellEditingStyle)行的编辑样式索引路径:(NSIndexPath*)索引路径{
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
}
}
*/
由于不太了解您的代码,我无法告诉您确切的问题,但是: 在顶部,您可以根据
搜索
的值来区分两个不同的数组。稍后,您将只使用当搜索
为false时使用的数组,该数组具有相同的索引,它似乎是NSDictionary
s的数组,而不是NSString
s的数组。您似乎第一次使用indexPath.section
访问它,第二次使用indexPath.row
访问它
我特别要说的是这一行:
if ([[listOfItems objectAtIndex:indexPath.row] isEqual:@"neon"]){
基于前面的代码,即此行:
NSDictionary *dictionary = [listOfItems objectAtIndex:indexPath.section];
我猜
listOfItems
包含的项目数等于表视图中的节数,而不是单击的节中的行数,我还猜返回值永远不会等于@“neon”
因为它是一个NSDictionary
根据您得到的异常,您正在调用objectAtIndex:
,值为60,并且NSMutableArray
中没有任何内容(范围0..0)。在使用推送代码之前,需要先解决此问题
当异常发生时,您应该能够从调试器中的调用堆栈中判断正在访问哪个数组,或者您可以在执行objectAtIndex:之前添加NSLog以查看您有什么。例如,您有:
if ([[listOfItems objectAtIndex:indexPath.row] isEqual:@"neon"]){
如果在上面一行之前添加下一行,您将能够看到您试图访问的索引(indexath.row)以及ListoItems中实际有多少索引:
NSLog(@"listOfItems has %d, accessing %d", [listOfItems count], indexPath.row);
任何想知道的人都可以使用此代码解决。 -(void)tableView:(UITableView*)tableView未选择RowatineXpath:(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];
}
}我添加了课程的其余部分作为编辑。我还是看不到要推的视图。通过提供的代码,您可以看到任何可以获得新视图的内容吗?如果唯一条目使用键“Elements”,您希望这个“Countries”数组来自哪里?它已更改为Elements。选择或推送过程中未发生任何更改。感谢您的回复。我点击了第三项,得到了:2010-05-09 19:43:49.392 iTeachU[4674:307]listOfItems有1个,访问3有没有办法告诉我使用第3行或类似的方式推送视图?你问错了问题。您应该问,为什么列表项中只有一项?如果这些答案中的任何一个解决了你的问题,或者有助于引导你朝正确的方向前进,那么最好选择一个,或者如果它们有帮助的话,就投上一票。问题的解决是因为它推动了这一观点。但是,搜索栏使用视图的副本。我可以让copyListItems和listItems只推送一个视图。选择“其他”将强制应用程序崩溃。我仍然需要一种方法来推送所有视图。通过声明一个字符串并设置isEqual,包括搜索在内的所有视图都可以使用。您的nslog代码有帮助。我将把它标记为答案,因为它引导我走上正确的道路。谢谢你的帮助。
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];
}