ios 无法将具有标识符的单元格出列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21589093/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 23:21:45  来源:igfitidea点击:

Unable to dequeue a cell with identifier

iosobjective-cuitableview

提问by thirdage

here's the method causing the problem:

这是导致问题的方法:

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

full log:

完整日志:

    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) 

回答by bsarr007

You can directly set the identifier using the storyboard like this.

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

enter image description here

在此处输入图片说明

回答by Dima

From the documentation:

从文档:

Important: You must register a class or nib file using the registerNib:forCellReuseIdentifier: or registerClass:forCellReuseIdentifier: method before calling this method.

重要提示:在调用此方法之前,您必须使用 registerNib:forCellReuseIdentifier: 或 registerClass:forCellReuseIdentifier: 方法注册一个类或 nib 文件。

You need to register the cell as a usable class for that identifier. Call this after you initialize your UITableView(probably in viewDidLoad)

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

[self.tableView registerClass:[TableCell class] forCellReuseIdentifier:CellIdentifier]

This assumes CellIdentifierhas been moved somewhere where it can be accessed from here.

这假设CellIdentifier已被移动到可以从这里访问的某个地方。

回答by Gago

Try this:

尝试这个:

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