2011-10-03 3 views
0

친구 목록에 파일 목록이 있습니다. UITableViewNavigation을 이해하려고합니다. 파일에서 목록을로드하지만 셀을 선택하면 해당 시점의 데이터 배열이 비어있는 것 같아서 왜 작동하지 않을 수 있습니다. 43 :이 방법에서
은이 오류를
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 얻을 08.411 navman2 [69,691 : B303] 앱 종단 * 인해 캐치되지 않는 예외 ' NSRangeException ', 이유 :'* - [NSMutableArray objectAtIndex :] : 빈 배열의 범위를 벗어나는 인덱스 2 '


여기

#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

인터페이스

#import <UIKit/UIKit.h> 

@interface TableViewController : UITableViewController 
{ 
    NSArray *dataArray; 
} 

@property(retain,nonatomic) NSArray *dataArray; 

@end 

다음 implementaion 어레이 전에 선택하는 올바른지 확인한 경우, 나는 시도 것이 우선이다 코드를 조금 재정렬하려면

- (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

배열이 자동 렌더링되는 것이 원인 일 수 있습니까? UIViewTable은 잘 채워지지만 배열은 비어 있습니다. 코드를 사용해보십시오. 배열 초기화에 대해 어떻게 생각하니 정확하지 않습니다. –

+0

예, viewDidLoad에 배열을 할당 할 때 self.dataArray를 사용할 수 있습니다. 내가 올바르게 기억하면 이것은 재산에 대한 소유권을 요구할 것입니다. – sosborn

관련 문제