-1

カスタム UITableViewCell アプリを読み込もうとするとクラッシュします。これまで何度もやっていましたが、今回も同じようにしました。クラッシュの原因がコメントされているコードの一部 (// この部分はクラッシュを引き起こしています):

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {


    static NSString *CellIdentifier = @"CustomTableCell";

    CustomTableCell *cell = (CustomTableCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {

       // This part is causing crash
        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomTableCell" owner:nil options:nil];

        cell = [topLevelObjects objectAtIndex:0];

    }

     return cell;

}

CustomTableCell.h、CustomTableCell.m、CustomTableCell.xib があります。

CellIdentifier は CustomTableCell であり、ペン先名にスペースの問題はありません。

エラーのスクリーンショット: ここに画像の説明を入力

クラッシュの原因は何ですか?

4

5 に答える 5

1

クラスのファイルの所有者ではなく、カスタムセルの接続を確認しましたか。? ここに画像の説明を入力

于 2013-03-15T12:43:55.073 に答える
0

これを試してみてください..

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   CustomTableCell *cell;
   cell = (CustomTableCell *)[tableView dequeueReusableCellWithIdentifier:@"CustomTableCell"];

   NSArray *nib;
   for (UIControl *subview in cell.contentView.subviews) {
      [subview removeFromSuperview];
   }

   if(cell == nil)
   {
       nib = [[NSBundle mainBundle] loadNibNamed:@"CustomTableCell" owner:self options:nil];

       for(id oneobject in nib)
       {
          if([oneobject isKindOfClass:[CustomTableCell class]])
          {
             cell = (CustomTableCell *)oneobject;
          }
       }
   }
}

それが役に立てば幸い..

于 2013-03-15T12:22:22.600 に答える
0

解決しました。

カスタムセル用の新しいxibファイルを作成しましたが、すべて問題ありません。

于 2013-03-15T12:42:52.040 に答える
0

次のコードを試してください:

#import "customCell.h"

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   // Configure the cell.
   static NSString *CustomCellIdentifier = @"customCell";
   customCell *cell = (customCell *)[tableView dequeueReusableCellWithIdentifier: CustomCellIdentifier];

   if (cell == nil)
   {
      NSArray *nib;
      nib = [[NSBundle mainBundle] loadNibNamed:@"customCell" owner:self options:nil];

      for (id oneObject in nib)
          if ([oneObject isKindOfClass:[customCell class]])
             cell = (customCell *)oneObject;

      cell.lbl.text = @"Hello";
   }
   return cell;
}

うまくいけば、それはあなたを助けるでしょう.

于 2013-03-15T12:17:34.553 に答える