0

我正在制作一个列出 Documents 字典中文件的 iOS。我想在 UITableView 中显示这些数据,问题是它不起作用。它将数据加载到视图中。然后应用程序冻结并调用EXC_BAD_ACCESS
这是我的代码:

- (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];
}
4

3 回答 3

1

This displays a basic misunderstanding of the concept of the table view data source.

My suggestion:

Create an array that contains the files outside any tableview methods. Then you use that array to feed the tableview.

Use array.count to return numberofrowsatindexpath. Also, on providing the cells at cellforrowatindexpath, don't use iterations/counters. The tableview ask for each element of your array itself using the indexpath argument. You access it like this:

id object = [array objectArIndex: indexPath.row]

And you then use the objects attributes to set the labels of the cell.

Please read a tutorial on tableviews. I recommend itunesU Paul hegarty's lectures. They're really great.

Ps. You release the bundleRoot object in numberofrowsinsection, which you don't retain (or use at all) which is most likely causing your crash.

Edit:

With a little more spare time, I retouched your code:

//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];
}
于 2012-07-09T17:04:18.483 回答
0

My problem was sort of like Mario's answer. [dirContents count] returned a higher value than the amount of times it could add an object from the array.

In other words it was a Out of Bounds error.

于 2012-07-09T17:07:00.130 回答
0

确保您在使用它时没有发布 dirContents。我确定您没有这样做,只是交叉检查您的代码。

于 2012-07-09T17:09:41.567 回答