iphone - UITableView 对象索引

标签 iphone cocoa-touch uitableview uinavigationbar

我正在开发一个 iPhone 应用程序,它是一个 tabViewController,其中一个选项卡包括一个 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:]: unrecognized selector sent to instance 0x601e800 2010-11-22 12:50:17.116 SSARC App 2[43470:207] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITableView objectAtIndex:]: unrecognized selector sent to instance 0x601e800' * Call stack at first throw:

现在,我收到一条警告,提示“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 被初始化。这是否意味着它找不到我所指的类(class)?请告诉我。

最佳答案

假设 table 是您的 UITableView,则对 [table objectAtIndex:indexPath.row] 的调用完全是无意义的。在我看来,您应该改为调用 [pages objectAtIndex:indexPath.row]

关于iphone - UITableView 对象索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4250301/

相关文章:

ios - 如何从一行开始并在新 View Controller 中显示该行的名称?

iphone - 如何在 tableView.reloadData 之后更改 uitableviewcell 的高度

ios - 当用户按下主页按钮时,应用程序究竟发生了什么?

iphone - 访问 UIImagePickerController 拍摄的图像的元数据(exif 标签)- iOS/iPhone

iphone - 如何使用 Cocoa (iPhone) 解码 XML 文档中的 shift-jis 编码数据

objective-c - iOS 应用程序在后台任务结束后运行

ios - 在 didSelectRowAtIndexPath 中动画 NSLayoutConstraint 不起作用

swift - RxSwift + canMoveRowAt

iphone - 尽管应用程序在前台,但 UIApplicationState 返回 Inactive

ios - 在 Objective-c 中解压并保存 bz2 文件