3

iOSアプリケーションにはUITableViewがあります。メソッドでは、cellForRowAtIndexPathNIB 名を使用してカスタム セルを返す必要があります。そのために私は使用しますloadNibNamed。(「willDisplayCellforRowAtIndexPath」にロードした後、セルにデータを入力します)

MyItemCell は、2 つの UIImageView と UIButton を含む XIB ファイル (MyItemCell.xib) です (各アイテムにはタグがあります)。

これは私のコードです:

私のviewControllerで

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [ViewHelper loadCustomCellWithNibName:@"MyItemCell" owner:self];
}

そしてカスタムセルをNIBからロードする方法

+ (UITableViewCell *) loadCustomCellFromNib:(NSString *)nibName owner:(id)owner
{
    UITableViewCell *cell = nil;
    NSArray *nibObjects = [[NSBundle mainBundle] loadNibNamed:nibName owner:owner options:nil];
    if([nibObjects count] > 0 )
    {
        cell = [nibObjects objectAtIndex:0];
    }
    else
    {
        NSLog(@"Failed to load %@ XIB file!", nibName);
    }
    return cell;
}

すべてのテストですべてが正しく機能します。ただし、一部のユーザーから、再現できないクラッシュが発生しました。

これはクラッシュです:

NSInternalInconsistencyException

Could not load NIB in bundle: 'NSBundle </var/mobile/Applications/7A24cE79-131F-523F-4C00-23B523ARG123/MyApp.app> (loaded)' with name 'MyItemCell'

スタック トレース:

0 CoreFoundation                        0x39b432a3 __exceptionPreprocess + 163 + 162

1 libobjc.A.dylib                       0x33a3297f objc_exception_throw + 31 + 30

2 CoreFoundation                        0x39b431c5 -[NSException initWithCoder:] + 1

3 UIKit                                 0x32e12491 -[UINib instantiateWithOwner:options:] + 1637 + 1636

4 UIKit                                 0x32e1a1d7 -[NSBundle(UINSBundleAdditions) loadNibNamed:owner:options:] + 139 + 138

5 MyApp                                 0x00047ded +[ViewHelper loadCustomCellFromNib:owner:] (ViewHelper.m:349)

6 MyApp                                 0x00034003 -[BuildViewController tableView:cellForRowAtIndexPath:] (BuildViewController.m:2432)

7 UIKit                                 0x32cc0545 -[UITableView(UITableViewInternal) _createPreparedCellForGlobalRow:withIndexPath:] + 413 + 412

8 UIKit                                 0x32ca530b -[UITableView(_UITableViewPrivate) _updateVisibleCellsNow:] + 1311 + 1310

9 UIKit                                 0x32cbc7c7 -[UITableView layoutSubviews] + 207 + 206

10 UIKit                                0x32c78803 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 259 + 258 

問題は、このクラッシュを再現できなかったことです。

クラッシュの原因について何か考えはありますか? または、そのようなエラーを回避するための解決策はありますか?

助けてくれてありがとう

編集:

詳細を明確にするために、これは私が行っているすべてのテストで完全に機能しています。このクラッシュは 1 人のユーザーに対して 1 回だけ発生したため、コードに問題はありません。非常に特定のシナリオでこのクラッシュを引き起こす可能性のある理由を探しています。ありがとう

4

2 に答える 2

12

NIBファイルからカスタムセルを作成するには、これを試してください

- (void)viewDidLoad
{
    [tableView registerNib:[UINib nibWithNibName:CellIdentifier bundle:nil] forCellReuseIdentifier:CellIdentifier];
}

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

    MyCell *cell = (MyCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    ......
    return cell;
}

CellIdentifier は NIB ファイルの名前で、セル識別子としても使用されます。

于 2013-01-25T11:32:04.090 に答える
1

これを試してください...問題はありません...

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   static NSString *CellIdentifier = @"albumListCell";
   albumListCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
   if (cell == nil)
   {
         cell = (albumListCell *) [[[NSBundle mainBundle] loadNibNamed:@"albumListCell" owner:self options:nil] lastObject];
   }
   return cell;
}

nibファイルでtableViewCellを作成することはできません..簡単なViewコントローラーを作成してから、UIViewControllerをUITableViewCellに変更してください...

于 2013-01-25T11:19:08.787 に答える