0

ドキュメント辞書のファイルを一覧表示する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

これは、テーブルビューデータソースの概念の基本的な誤解を示しています。

私のおすすめ:

tableviewメソッドの外部にあるファイルを含む配列を作成します。次に、その配列を使用してテーブルビューにフィードします。

array.countを使用して、numberofrowsatindexpathを返します。また、cellforrowatindexpathにセルを提供する場合は、反復/カウンターを使用しないでください。テーブルビューは、indexpath引数を使用して、配列自体の各要素を要求します。次のようにアクセスします。

idオブジェクト=[配列objectArIndex:indexPath.row]

次に、objects属性を使用して、セルのラベルを設定します。

テーブルビューのチュートリアルをお読みください。itunesUPaulhegartyの講義をお勧めします。彼らは本当に素晴らしいです。

追伸 bundleRootオブジェクトをnumberofrowsinsectionで解放しますが、これは保持しない(またはまったく使用しない)ため、クラッシュの原因となる可能性があります。

編集:

もう少し暇な時間で、私はあなたのコードをレタッチしました:

//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

それを使用している間はdirContentsをリリースしていないことを確認してください。リリースしていないと確信していますが、コードとクロスチェックしてください。

于 2012-07-09T17:09:41.567 に答える
0

私の問題はマリオの答えのようなものでした。配列からオブジェクトを追加できる回数よりも高い値を[dirContents count]返しました。言い換えれば、それは範囲外エラーでした。

于 2012-07-09T17:07:00.130 に答える