UITableView ObjectAtIndex

时间:2010-11-22 21:16:47

标签: iphone cocoa-touch uitableview uinavigationbar

我正在开发一个带有tabViewController的iPhone应用程序,其中一个选项卡包括tableView。可以触摸表格中的对象以转到具有该特定对象的信息的初始屏幕。

以下是我如何设置数组tableView:

 -(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

 //each table view cell has its own identifier. 
 static NSString *cellIdentifier = @"CellIdentifier"; 
 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 

 if (cell == nil)
 {
  cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier] autorelease];
 }

 NSUInteger row = [indexPath row]; 

 cell.textLabel.text = [sitesArray objectAtIndex:row];
 cell.imageView.image = [imagesArray objectAtIndex:row];

 cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;

 return cell; 
}

    - (void)viewDidLoad {

  NSArray *pageHolder = [[NSArray alloc] initWithObjects:@"CSLP", @"NSLS", @"LSPA", @"SIOP", @"Transfer", nil];
  self.pages = pageHolder;
  [pageHolder release]; 

  NSArray *sites = [[NSArray alloc] initWithObjects:@"CSLP", @"NSLS", @"LSPA", @"SIOP", @"Transfer Vision Program ", nil];
 self.sitesArray = sites; 

 UIImage *cslp = [UIImage imageNamed:@"CSLP LOGO.png"]; 
 UIImage *nsls = [UIImage imageNamed:@"NSLS LOGO.png"]; 
 UIImage *lspa = [UIImage imageNamed:@"LSPA LOGO.png"]; 
 UIImage *siop = [UIImage imageNamed:@"SIOP LOGO.png"]; 
 UIImage *transfer = [UIImage imageNamed:@"TRANSFER LOGO.png"]; 
 NSArray *images = [[NSArray alloc] initWithObjects: cslp, nsls, lspa, siop, transfer, nil]; 
 [sites release]; 
 self.imagesArray = images; 

 UIButton* modalViewButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
 [modalViewButton addTarget:self action:@selector(modalViewAction:) forControlEvents:UIControlEventTouchUpInside];
 UIBarButtonItem *modalBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:modalViewButton];
 self.navigationItem.rightBarButtonItem = modalBarButtonItem;
 [modalBarButtonItem release];

 [self.table reloadData];
}

这是我的didSelectRowAtIndexPath方法

-(void) tableView:(UITableView *) tableView didSelectRowAtIndexPath: (NSIndexPath *) indexPath
{
 NSInteger row = [indexPath row];

 NSMutableDictionary *rowData = [table objectAtIndex:indexPath.row];
 UIViewController *targetViewController = [rowData objectForKey:kViewControllerKey];
 if (!targetViewController)
 {
        // The view controller has not been created yet, create it and set it to our menuList array
        NSString *viewControllerName = [[pages objectAtIndex:indexPath.row] stringByAppendingString:@"ViewController"];
        targetViewController = [[NSClassFromString(viewControllerName) alloc] initWithNibName:viewControllerName bundle:nil];
        [rowData setValue:targetViewController forKey:kViewControllerKey];
  [targetViewController release];
    }

 SSARC_App_2AppDelegate *delegate = [[UIApplication sharedApplication] delegate]; 
 [delegate.orgsNavigationController pushNavigationItem:targetViewController animated:YES];

 [delegate release];
 }

应用程序在didSelectRowAtIndexPath方法中第一次调用objectAtIndex时崩溃。控制台声明:

  

010-11-22 12:50:17.113 SSARC App   2 [43470:207] - [UITableView   objectAtIndex:]:无法识别的选择器   发送到实例0x601e800 2010-11-22   12:50:17.116 SSARC App 2 [43470:207]    *因未捕获的异常而终止应用   'NSInvalidArgumentException',原因:   ' - [UITableView objectAtIndex:]:   无法识别的选择器发送到实例   0x601e800'   * 首先调用堆栈:

现在,我收到一条警告,说“UITableView可能无法响应ObjectAtIndex”。我假设这是这里的主要问题,但我不确定如何解决它。有趣的是,我到处搜索这个问题,并且无法在任何地方找到它,所以我很想知道是否有人遇到过这个问题以及如何解决它。任何建议都会很棒。如果您需要更多信息,请与我们联系。


我自己一直在做一些编码,我已经让它编译没有任何错误或警告,但它仍然崩溃。目前这就是我所拥有的:

NSMutableDictionary *rowData = [self.menuList objectAtIndex:indexPath.row];
UIViewController *targetViewController = [rowData objectForKey:kViewControllerKey];
if (!targetViewController)
{
    // The view controller has not been created yet, create it and set it to our menuList array

NSString *viewControllerName = [[pages objectAtIndex:indexPath.row] stringByAppendingString:@"ViewController"];

targetViewController = [[NSClassFromString(viewControllerName) alloc] init];//WithNibName:viewControllerName bundle:nil];
    NSLog(@"targetViewController: %@", targetViewController); 

    [rowData setValue:targetViewController forKey:kViewControllerKey];
    [targetViewController release];
}


SSARC_App_2AppDelegate *delegate = (SSARC_App_2AppDelegate*)[[UIApplication sharedApplication] delegate]; 
[[delegate orgsNavigationController] pushViewController:targetViewController animated:YES];

[delegate release]; 

delegate = nil; 

调试器报告的问题是'viewControllerName'是'超出范围'。我真的不明白这意味着什么,因为当我使用NSLog时,viewControllerName被初始化。这是否意味着找不到我所指的课程?请告诉我。

2 个答案:

答案 0 :(得分:1)

假设tableUITableView,则对[table objectAtIndex:indexPath.row]的调用完全是荒谬的。在我看来,你应该改为呼叫[pages objectAtIndex:indexPath.row]

答案 1 :(得分:0)

应该为NSMutableArray或NSArray调用

objectAtIndex 。你不能从UITableview或CFDictionary或CFString中调用它。因此,如果您收到任何警告,请先解决警告,然后再继续。