2010-11-22 67 views
2

我正在開發一個iPhone應用程序,它是一個tabViewController,其中包括一個tableView的選項卡之一。可以觸摸表格中的對象以轉到具有該特定對象信息的啓動畫面。UITableView ObjectAtIndex

下面是如何設置的陣列的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應用 2 [43470:207] - [UITableView的 objectAtIndex:]:發送到實例無法識別選擇 0x601e800 2010-11-22 12:50:17.116 SSARC應用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被初始化。這是否意味着它找不到我指的類?請告訴我。

+0

你可以顯示`sitesArray`屬性聲明嗎 – vikingosegundo 2010-11-22 22:19:54

+0

你可以顯示`sitesArray`,`imagesArray`和`頁面的屬性聲明嗎? – vikingosegundo 2010-11-22 22:25:58

回答

1

假設table是您的UITableView,致電[table objectAtIndex:indexPath.row]是完全荒謬的。它看起來像你應該調用[pages objectAtIndex:indexPath.row]

0

objectAtIndex應該調用NSMutableArray或NSArray。你不能從UITableview或CFDictionary或CFString中調用它。因此,如果您收到任何警告,請先解決警告,然後再繼續操作。