2012-07-09 62 views
0

我正在做一個iOS列出文檔字典中的文件。我想在UITableView中顯示這些數據,問題在於它無法正常工作。 它將數據加載到視圖中。 然後應用凍結並調用EXC_BAD_ACCESS
這是我的代碼:UITableView顯示數據,然後崩潰

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ 
    timesRun = 0; 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectoryPath = [paths objectAtIndex:0]; 
    NSString *bundleRoot = [[NSBundle mainBundle] bundlePath]; 
    dirContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectoryPath error:nil]; 
    [bundleRoot release]; 
    NSLog(@"Count: %i",[dirContents count]); 
    return [dirContents count]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableViewData cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    UITableViewCell *cell = nil; 
    int Locatation = indexPath.row; 
    Locatation++; 
    cell = [tableViewData dequeueReusableCellWithIdentifier:@"MyCells"]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MyCell"]autorelease]; 
    } 
    //cell.textLabel.text = [NSString stringWithFormat:@"Cell: %i",Locatation]; 
    cell.textLabel.text = [dirContents objectAtIndex:timesRun]; 
    timesRun++; 
    return cell; 
    NSLog(@"Did return"); 
} 
- (void)tableView:(UITableView *)tableViewDat didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
    NSLog(@"%@",cell.textLabel.text); 
    [tableView deselectRowAtIndexPath:indexPath animated:YES]; 
} 

回答

0

我的問題有點像馬里奧的回答。 [dirContents count]返回一個高於的值,而不是它可以從數組中添加對象的次數。

換句話說,這是一個超出界限的錯誤。

+0

是的。但我認爲越界是來自你的timesRun ++,它每次調用cellForRowAtIndexpath時都會增加(這是每次表需要檢查當前顯示的單元格時,最有可能比你的數組大小更多)。你真的不需要自己保留一個櫃檯(並且它也不像你所做的那樣工作)。如果你有更多的跟進qstns,請隨時在這裏問。 – Mario 2012-07-09 18:43:48

1

這將顯示錶視圖數據源的概念的基本誤解。

我的建議:

創建一個包含任何實現代碼如下方法之外文件的數組。然後你使用該數組來提供tableview。

使用array.count返回numberofrowsatindexpath。 另外,在cellforrowatindexpath上提供細胞時,請勿使用迭代/計數器。 tableview使用indexpath參數詢問數組的每個元素。你訪問它是這樣的:

ID對象= [數組objectArIndex:indexPath.row]

然後使用對象的屬性來設置單元格的標籤。

請閱讀有關tableviews的教程。我推薦itunesU Paul Hegarty的講座。他們真的很棒。

Ps。您在numberofrowsinsection中釋放了bundleRoot對象,您不會保留(或完全使用)最有可能導致崩潰的對象。

編輯:

有了一點空閒時間,我潤飾代碼:

//code not tested/written in xcode. Might contain typos 

//in your .m file 
//in your (private) interface 

@property (nonatomic, retain, readonly) NSArray *dirContents; 

//in your implementation 

@synthesize dirContents=_dirContents; 

-(NSArray*) dirContents { 
    if (!dirContents) { 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectoryPath = [paths objectAtIndex:0]; 
    _dirContents = [[[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectoryPath error:nil] retain]; 
    } 
    return _dirContents; 
    //note that if you want to "refresh" the contents you would have to 
    //release _dirContents and set it to nil or implement this differently 
} 

-(void) dealloc { 
    [_dirContents release]; 
    //.... 
    [super dealloc]; 
} 


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ 
    return [[self dirContents] count]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    UITableViewCell *cell = nil; 

    cell = [tableView dequeueReusableCellWithIdentifier:@"MyCells"]; 
    if (cell == nil) { 
    cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MyCells"]autorelease]; 
    } 

    cell.textLabel.text = [[self dirContents] objectAtIndex: indexPath.row]; 

    return cell; 
    //  NSLog(@"Did return"); //this never gets called by the way 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
    NSLog(@"%@",cell.textLabel.text); 
    [tableView deselectRowAtIndexPath:indexPath animated:YES]; 
} 
0

確保在使用時不會釋放dirContents。我確信您沒有做,只是與您的代碼進行交叉檢查。