2011-10-03 33 views
0

我有一些朋友的名字的文件列表。我想了解UITableViewNavigation。我從一個文件加載列表,但是當我選擇一個單元格時,數據數組在那一點上似乎是空的,我無法弄清楚爲什麼。
在該方法我得到這個錯誤
爲什麼在選擇UITableView單元時NSArray爲空?

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
    { 

     FriendsNameViewController *newView =[[FriendsNameViewController alloc] initWithNibName:@"FriendsNameViewController" bundle:nil]; 

     [self.navigationController pushViewController:newView animated:YES]; 
     //Crashes here 
     NSString *temp = [dataArray objectAtIndex:indexPath.row]; 
     NSLog(@"%@",temp); 
     [[newView labelx] setText:[NSString stringWithFormat: @"%@" ,temp]]; 
    } 


2011-10-03 14:43:08.411 navman2 [69691:B303] *終止應用程序由於 未捕獲的異常' NSRangeException',原因:'* - [NSMutableArray objectAtIndex:]:索引2超出空數組的界限'


接口

#import <UIKit/UIKit.h> 

@interface TableViewController : UITableViewController 
{ 
    NSArray *dataArray; 
} 

@property(retain,nonatomic) NSArray *dataArray; 

@end 

然後implementaion這裏

#import "TableViewController.h" 
#import "FriendsNameViewController.h" 

@implementation TableViewController 
@synthesize dataArray; 

- (id)initWithStyle:(UITableViewStyle)style 
{ 
    self = [super initWithStyle:style]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 

- (void)didReceiveMemoryWarning 
{ 
    // Releases the view if it doesn't have a superview. 
    [super didReceiveMemoryWarning]; 

    // Release any cached data, images, etc that aren't in use. 
} 

#pragma mark - View lifecycle 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"myfriendsList" ofType:@"txt"]; 
    NSStringEncoding encoding; 
    NSError* error; 
    NSString* fh = [NSString stringWithContentsOfFile:filePath usedEncoding:&encoding error:&error]; 
    dataArray = [NSArray alloc]; 
    dataArray = [fh componentsSeparatedByString:@"\n"]; 
} 

- (void)viewDidUnload 
{ 
    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 
    // e.g. self.myOutlet = nil; 
} 

- (void)viewWillAppear:(BOOL)animated 
{ 
    [super viewWillAppear:animated]; 
} 

- (void)viewDidAppear:(BOOL)animated 
{ 
    [super viewDidAppear:animated]; 
} 

- (void)viewWillDisappear:(BOOL)animated 
{ 
    [super viewWillDisappear:animated]; 
} 

- (void)viewDidDisappear:(BOOL)animated 
{ 
    [super viewDidDisappear:animated]; 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    // Return YES for supported orientations 
    return (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 

#pragma mark - Table view data source 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    // Return the number of sections. 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    // Return the number of rows in the section. 
    return [dataArray count]; 
} 

- (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... 
    cell.textLabel.text = [dataArray objectAtIndex:indexPath.row]; 

    return cell; 
} 



#pragma mark - Table view delegate 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    FriendsNameViewController *newView =[[FriendsNameViewController alloc] initWithNibName:@"FriendsNameViewController" bundle:nil]; 

    [self.navigationController pushViewController:newView animated:YES]; 
    //Crashes here 
    NSString *temp = [dataArray objectAtIndex:indexPath.row]; 
    NSLog(@"%@",temp); 
    [[newView labelx] setText:[NSString stringWithFormat: @"%@" ,temp]]; 
} 

@end 
+0

將'viewDidunload'中的代碼更改爲'self.dataArray = [NSMutableArray alloc];' 'self.dataArray = [fh componentsSeparatedByString:@「\ n」];'修復了它,但我沒有得到區別。知道它與Autorelease有關,但是自我產生了什麼差別。 –

回答

1

如果您已經驗證該陣列是否選擇之前正確的,那麼我會嘗試的第一件事是重新排列你的代碼一點點:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
    { 

     FriendsNameViewController *newView =[[FriendsNameViewController alloc] initWithNibName:@"FriendsNameViewController" bundle:nil];  
     NSString *temp = [dataArray objectAtIndex:indexPath.row]; 
     NSLog(@"%@",temp); 
     [[newView labelx] setText:[NSString stringWithFormat: @"%@" ,temp]]; 
     [self.navigationController pushViewController:newView animated:YES]; 
    } 
+0

這是否是由於數組越來越autoreleased?UIViewTable填充罰款,但然後數組變得empty.Will嘗試你的code.What你認爲可能是我的初始化數組是不正確的你認爲。 –

+0

是的,當您在viewDidLoad中分配數組時,您可能需要使用self.dataArray。如果我沒有記錯的話,這會調用保留在屬性上。 – sosborn

相關問題