无法使用标识符将单元格出列

时间:2014-02-05 21:39:07

标签: ios objective-c uitableview

这是造成问题的方法:

-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"TableCell";
    TableCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier 
                                                      forIndexPath:indexPath];

    // Configure the cell...
    int row = [indexPath row];

    Books *book = [myBooks objectAtIndex:row];
    cell.bookName.text = book.bookName;

    return cell;
}

完整日志:

    2014-02-05 23:19:09.458 Books[1425:a0b] *** Assertion failure in -[UITableView dequeueReusableCellWithIdentifier:forIndexPath:], /SourceCache/UIKit_Sim/UIKit-2903.2/UITableView.m:5251
2014-02-05 23:19:09.471 Books[1425:a0b] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'unable to dequeue a cell with identifier TableCell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'
*** First throw call stack:
(
    0   CoreFoundation                      0x017395e4 __exceptionPreprocess + 180
    1   libobjc.A.dylib                     0x014bc8b6 objc_exception_throw + 44
    2   CoreFoundation                      0x01739448 +[NSException raise:format:arguments:] + 136
    3   Foundation                          0x0109d23e -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 116
    4   UIKit                               0x003135e3 -[UITableView dequeueReusableCellWithIdentifier:forIndexPath:] + 170
    5   Books                               0x000032df -[TableViewController tableView:cellForRowAtIndexPath:] + 127
    6   UIKit                               0x0031dd2f -[UITableView _createPreparedCellForGlobalRow:withIndexPath:] + 412
    7   UIKit                               0x0031de03 -[UITableView _createPreparedCellForGlobalRow:] + 69
    8   UIKit                               0x00302124 -[UITableView _updateVisibleCellsNow:] + 2378
    9   UIKit                               0x003155a5 -[UITableView layoutSubviews] + 213
    10  UIKit                               0x00299dd7 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 355
    11  libobjc.A.dylib                     0x014ce81f -[NSObject performSelector:withObject:] + 70
    12  QuartzCore                          0x03af372a -[CALayer layoutSublayers] + 148
    13  QuartzCore                          0x03ae7514 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 380
    14  QuartzCore                          0x03af3675 -[CALayer layoutIfNeeded] + 160
    15  UIKit                               0x00354ca3 -[UIViewController window:setupWithInterfaceOrientation:] + 304
    16  UIKit                               0x00273d27 -[UIWindow _setRotatableClient:toOrientation:updateStatusBar:duration:force:isRotating:] + 5212
    17  UIKit                               0x002728c6 -[UIWindow _setRotatableClient:toOrientation:updateStatusBar:duration:force:] + 82
    18  UIKit                               0x00272798 -[UIWindow _setRotatableViewOrientation:updateStatusBar:duration:force:] + 117
    19  UIKit                               0x00272820 -[UIWindow _setRotatableViewOrientation:duration:force:] + 67
    20  UIKit                               0x002718ba __57-[UIWindow _updateToInterfaceOrientation:duration:force:]_block_invoke + 120
    21  UIKit                               0x0027181c -[UIWindow _updateToInterfaceOrientation:duration:force:] + 400
    22  UIKit                               0x00272573 -[UIWindow setAutorotates:forceUpdateInterfaceOrientation:] + 870
    23  UIKit                               0x00275b66 -[UIWindow setDelegate:] + 449
    24  UIKit                               0x00346dc7 -[UIViewController _tryBecomeRootViewControllerInWindow:] + 180
    25  UIKit                               0x0026b7cc -[UIWindow addRootViewControllerViewIfPossible] + 609
    26  UIKit                               0x0026b947 -[UIWindow _setHidden:forced:] + 312
    27  UIKit                               0x0026bbdd -[UIWindow _orderFrontWithoutMakingKey] + 49
    28  UIKit                               0x0027644a -[UIWindow makeKeyAndVisible] + 65
    29  UIKit                               0x002298e0 -[UIApplication _callInitializationDelegatesForURL:payload:suspended:] + 1851
    30  UIKit                               0x0022dfb8 -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 824
    31  UIKit                               0x0024242c -[UIApplication handleEvent:withNewEvent:] + 3447
    32  UIKit                               0x00242999 -[UIApplication sendEvent:] + 85
    33  UIKit                               0x0022fc35 _UIApplicationHandleEvent + 736
    34  GraphicsServices                    0x0368c2eb _PurpleEventCallback + 776
    35  GraphicsServices                    0x0368bdf6 PurpleEventCallback + 46
    36  CoreFoundation                      0x016b4dd5 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 53
    37  CoreFoundation                      0x016b4b0b __CFRunLoopDoSource1 + 523
    38  CoreFoundation                      0x016df7ec __CFRunLoopRun + 2156
    39  CoreFoundation                      0x016deb33 CFRunLoopRunSpecific + 467
    40  CoreFoundation                      0x016de94b CFRunLoopRunInMode + 123
    41  UIKit                               0x0022d6ed -[UIApplication _run] + 840
    42  UIKit                               0x0022f94b UIApplicationMain + 1225
    43  Books                               0x000038bd main + 141
    44  libdyld.dylib                       0x01d75725 start + 0
    45  ???                                 0x00000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

3 个答案:

答案 0 :(得分:22)

您可以使用这样的故事板直接设置标识符。

enter image description here

答案 1 :(得分:12)

来自文档:

  

重要说明:您必须使用以下命令注册类或nib文件   registerNib:forCellReuseIdentifier:或   registerClass:forCellReuseIdentifier:调用它之前的方法   方法

您需要将单元格注册为该标识符的可用类。在初始化UITableView(可能在viewDidLoad

后调用此方法
[self.tableView registerClass:[TableCell class] forCellReuseIdentifier:CellIdentifier]

这假定CellIdentifier已被移动到可从此处访问的地方。

答案 2 :(得分:3)

试试这个:

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

static NSString *CellIdentifier = @"TableCell";
TableCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if(!cell)
{
    cell = [[[TableCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}

// Configure the cell...
int row = [indexPath row];

Books *book = [myBooks objectAtIndex:row];
cell.bookName.text = book.bookName;

return cell;
}