2013-09-28 51 views
-3

當前正在考慮使用io6構建的清單教程。我正在使用xcode 5,ios7 sdk。該教程尚未更新IOS7,但我不想停止我的學習,因此決定繼續前進並使用過時的教程,並希望將其用作學習體驗。通過閱讀官方Apple文檔和廣泛的Google搜索作爲我的指導。這個斷言失敗在UITableViewDataSource中意味着什麼?

很早,我已經遇到了一個問題,不知道什麼是錯的。我注意到自動完成功能已將部分方法刪除(可能會棄用)。這個問題肯定來自下面的代碼,因爲一旦我刪除它,模擬器加載應用程序的罰款。

代碼導致崩潰:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return 1; 
} 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ChecklistItem"]; 
    return cell; 
} 

這裏是堆棧跟蹤:

2013-09-28 20:51:26.208 Checklists[47289:a0b] *** Assertion failure in -[UITableView _configureCellForDisplay:forIndexPath:], /SourceCache/UIKit_Sim/UIKit-2903.2/UITableView.m:6235 
2013-09-28 20:51:26.218 Checklists[47289:a0b] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:' 
*** First throw call stack: 
(
    0 CoreFoundation      0x017335e4 __exceptionPreprocess + 180 
    1 libobjc.A.dylib      0x014b68b6 objc_exception_throw + 44 
    2 CoreFoundation      0x01733448 +[NSException raise:format:arguments:] + 136 
    3 Foundation       0x0109723e -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 116 
    4 UIKit        0x00311ae5 __53-[UITableView _configureCellForDisplay:forIndexPath:]_block_invoke + 426 
    5 UIKit        0x0028af5f +[UIView(Animation) performWithoutAnimation:] + 82 
    6 UIKit        0x0028afa8 +[UIView(Animation) _performWithoutAnimation:] + 40 
    7 UIKit        0x00311936 -[UITableView _configureCellForDisplay:forIndexPath:] + 108 
    8 UIKit        0x00317d4d -[UITableView _createPreparedCellForGlobalRow:withIndexPath:] + 442 
    9 UIKit        0x00317e03 -[UITableView _createPreparedCellForGlobalRow:] + 69 
    10 UIKit        0x002fc124 -[UITableView _updateVisibleCellsNow:] + 2378 
    11 UIKit        0x0030f5a5 -[UITableView layoutSubviews] + 213 
    12 UIKit        0x00293dd7 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 355 
    13 libobjc.A.dylib      0x014c881f -[NSObject performSelector:withObject:] + 70 
    14 QuartzCore       0x03aed72a -[CALayer layoutSublayers] + 148 
    15 QuartzCore       0x03ae1514 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 380 
    16 QuartzCore       0x03aed675 -[CALayer layoutIfNeeded] + 160 
    17 UIKit        0x0034eca3 -[UIViewController window:setupWithInterfaceOrientation:] + 304 
    18 UIKit        0x0026dd27 -[UIWindow _setRotatableClient:toOrientation:updateStatusBar:duration:force:isRotating:] + 5212 
    19 UIKit        0x0026c8c6 -[UIWindow _setRotatableClient:toOrientation:updateStatusBar:duration:force:] + 82 
    20 UIKit        0x0026c798 -[UIWindow _setRotatableViewOrientation:updateStatusBar:duration:force:] + 117 
    21 UIKit        0x0026c820 -[UIWindow _setRotatableViewOrientation:duration:force:] + 67 
    22 UIKit        0x0026b8ba __57-[UIWindow _updateToInterfaceOrientation:duration:force:]_block_invoke + 120 
    23 UIKit        0x0026b81c -[UIWindow _updateToInterfaceOrientation:duration:force:] + 400 
    24 UIKit        0x0026c573 -[UIWindow setAutorotates:forceUpdateInterfaceOrientation:] + 870 
    25 UIKit        0x0026fb66 -[UIWindow setDelegate:] + 449 
    26 UIKit        0x00340dc7 -[UIViewController _tryBecomeRootViewControllerInWindow:] + 180 
    27 UIKit        0x002657cc -[UIWindow addRootViewControllerViewIfPossible] + 609 
    28 UIKit        0x00265947 -[UIWindow _setHidden:forced:] + 312 
    29 UIKit        0x00265bdd -[UIWindow _orderFrontWithoutMakingKey] + 49 
    30 UIKit        0x0027044a -[UIWindow makeKeyAndVisible] + 65 
    31 UIKit        0x002238e0 -[UIApplication _callInitializationDelegatesForURL:payload:suspended:] + 1851 
    32 UIKit        0x00227fb8 -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 824 
    33 UIKit        0x0023c42c -[UIApplication handleEvent:withNewEvent:] + 3447 
    34 UIKit        0x0023c999 -[UIApplication sendEvent:] + 85 
    35 UIKit        0x00229c35 _UIApplicationHandleEvent + 736 
    36 GraphicsServices     0x036862eb _PurpleEventCallback + 776 
    37 GraphicsServices     0x03685df6 PurpleEventCallback + 46 
    38 CoreFoundation      0x016aedd5 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 53 
    39 CoreFoundation      0x016aeb0b __CFRunLoopDoSource1 + 523 
    40 CoreFoundation      0x016d97ec __CFRunLoopRun + 2156 
    41 CoreFoundation      0x016d8b33 CFRunLoopRunSpecific + 467 
    42 CoreFoundation      0x016d894b CFRunLoopRunInMode + 123 
    43 UIKit        0x002276ed -[UIApplication _run] + 840 
    44 UIKit        0x0022994b UIApplicationMain + 1225 
    45 Checklists       0x00001b7d main + 141 
    46 libdyld.dylib      0x01d6f725 start + 0 
) 
libc++abi.dylib: terminating with uncaught exception of type NSException 
+0

我問最後一個問題是也標誌着下來沒有理由。所以下次我可能會提出另一個問題而犯同樣的錯誤。 – LondonGuy

+0

您沒有閱讀異常原因。你的問題在那裏用簡單的英語。這可能會導致downvotes。只是猜測。 –

+0

如果我做了,並且不理解它會怎麼樣?還在學習這樣的事情,可能看起來超級明顯,你和其他人不可能如此明顯,我就是這個原因之後很長一段時間試圖弄清楚這一點,我來到這裏。 – LondonGuy

回答

1

我猜你沒有設置你的故事板細胞的識別符,它崩潰,因爲它不能實例化一個細胞與不存在

+0

這是問題..好樣的。我拼錯了標識符。編輯checklistItem checklistItem解決了這個問題。謝謝。 – LondonGuy

0

您需要設置類作爲數據源的實現代碼如下。您可以通過控制從故事板的tableview拖動到底部(最左側)的viewController圖標並單擊dataSource和委託來完成此操作。

1

dequeueReusableCellWithIdentifier:用於重複使用表格視圖的標識符細胞。如果沒有要重用的單元格,則此方法返回nil,您必須手動創建表格視圖單元格並將其返回。當有一個表格視圖單元格與先前使用的另一個表格單元格相同時,dequeueReusableCellWithIdentifier:可能會返回有效的單元格。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ChecklistItem"]; 
    if(!cell) 
     cell= [[UITableViewCell alloc]initWithStyle: UITableViewCellStyleDefault reuseIdentifier: @"ChecklistItem"]; 
    return cell; 
} 
+1

這是做這件事的老方法。 Apple通過在iOS 6上提供'dequeueReusableCellWithIdentifier:forIndexPath:'來大大簡化了這一點。假設您爲該標識符註冊了一個單元格,API將自動爲您實例化一個單元格。 –

1

這意味着

UITableView數據源必須從tableView:cellForRowAtIndexPath:

的錯誤消息指出返回一個細胞。

dequeueReusableCellWithIdentifier:不能保證總是返回電池,因爲它可能會返回nil

如果您支持iOS> = 6,請將其更改爲dequeueReusableCellWithIdentifier:forIndexPath:,這將永不返回nil

documentation很清楚。

要包起來你的代碼應該是

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ChecklistItem" 
                  forIndexPath:indexPath]; 
    /* Configure the cell here */ 
    return cell; 
}