我正在做一个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