2010-01-18 42 views
0

該應用程序非常簡單,一個導航到tableview的nevigationviewcontroller。 但當i'm訪問到langsarray,它崩潰訪問UITableView中的變量崩潰我的應用程序

下面是相關代碼:

MenuViewController.m 
-(void) Settings{ 
    TestViewController *testvc = [[TestViewController alloc] initWithNibName:nil bundle:nil]; 
    [self.navigationController setNavigationBarHidden:NO animated:NO]; 
    [self.navigationController pushViewController:testvc animated:YES]; 
    [testvc release]; 
} 

#import <UIKit/UIKit.h> 
@interface TestViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>   
{ 
    NSArray *langsarray; 
} 
@property (nonatomic, retain) NSArray *langsarray; 
@end 



#import "TestViewController.h" 

@implementation TestViewController 
@synthesize langsarray; 


- (void)loadView { 
    //Create the table 
    UITableView *tableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame] style:UITableViewStylePlain]; 
    tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth; 
    tableView.delegate = self; 
    tableView.dataSource = self;  
    [tableView reloadData];  
    self.view = tableView; 

    //read the info 
    NSString *path = [[NSBundle mainBundle] pathForResource:@"l" ofType:@"txt"]; 
    NSString *langs = [[NSString alloc]initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil]; 
    langsarray = [langs componentsSeparatedByString: @"\n"]; 

    [path release]; 
    [langs release]; 
    [tableView release]; 

} 


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 

    //Accessing langsarray crash the app 
    //harcoded works fine: return 5; 
    return [langsarray count]; 
} 

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

    static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: 
          SimpleTableIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] 
       initWithStyle:UITableViewCellStyleSubtitle 
       reuseIdentifier:SimpleTableIdentifier] autorelease]; 
    } 

    NSUInteger row = [indexPath row]; 
    // again, accessing langsarray crash the app 
    cell.textLabel.text = [[[langsarray objectAtIndex:row] componentsSeparatedByString:@"="] objectAtIndex:0]; 
    cell.detailTextLabel.text = [[[langsarray objectAtIndex:row] componentsSeparatedByString:@"="] objectAtIndex:1]; 

    return cell; 
} 

這是

計劃接收信號輸出: 「EXC_BAD_ACCESS」。警告:無法 找到 「_sigtramp」 最小符號 - 回溯可能是不可靠的殺

的Xcode 3.1.4 豹

回答

0

你的問題是在這條線:在右手側返回

langsarray = [langs componentsSeparatedByString: @"\n"]; 

值會被自動釋放,這意味着當你讀它以後本已被釋放。

請嘗試以下變化:

self.langsarray = [langs componentsSeparatedByString: @"\n"]; 

這將使使用屬性,它會自動保留它。你也可以考慮手動保留它。

更新迴應評論...

  1. 是什麼langs包含拆分成組件之前?你確定那裏有一個真正的字符串嗎? langsarray是否包含您在作業後立即期待的內容?

  2. 什麼是numberOfRowsInSection實際崩潰?您返回的值是否會導致崩潰,或者您是否計算了損壞的對象? (例如,對於用來在表中部分的數量返回零 - 並可能仍 - 導致崩潰。)

我分裂代碼出這樣的:

NSUInteger c = [self.langsarray count]; 
return c; 

這樣你可以設置一個斷點並看到c的值。

+0

感謝您的幫助,但它doesn't工作。 我把self.langsarray = [langs componentsSeparatedByString:@「\ n」]; inOfRowsInSection crash: return [self.langsarray count]; return [langsarray count];在兩種情況下都會發生碰撞。 – gerardp 2010-01-18 11:24:18

0

您需要retainlangsarray在這條線:

langsarray = [langs componentsSeparatedByString: @"\n"]; 

componentsSeparatedByString返回NSArray,你沒有自己的(沒有alloc,或使用方法與copy...new...alloc...),所以你需要保留/釋放它。

0

的問題:

編輯,一些調試

後我發現是自己做錯了什麼:

  • self.langsarray = [LANGS componentsSeparatedByString:@ 「\ n」];

  • // [path release]; // [langs release];

    但我懷疑是否有內存泄漏。

現在一切工作正常,

感謝斯蒂芬