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