【问题标题】:UITableView using UIViewController not loading tableViewUITableView 使用 UIViewController 不加载 tableView
【发布时间】:2017-06-20 03:21:56
【问题描述】:

我有以下布局,其中我的搜索栏位于顶部,并且不使用 tableView 滚动。我已经用我的自定义 UIViewControler 连接了 UITableView 的数据源和委托,但是我没有填充我的 tableview,而是我收到了下面的错误。请问有人可以建议吗?

我的布局:

TestTableViewController - 检查员:

TestTableViewController:

 class TestTableViewController: UIViewController, UITableViewDelegate, UISearchBarDelegate{


@IBOutlet var myTableView: UITableView!
var itemstore: ItemStore!

override func viewDidLoad() {
    super.viewDidLoad()


}


func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    print("numberOfRowsSection ...")
    return itemstore.allItems.count
}



func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    print("cellForRow ...")
    // Get a new or recycled cell
    let cell = UITableViewCell(style: .value1, reuseIdentifier: "UITableViewCell")
    let name = itemstore.allItems[indexPath.row]
    cell.textLabel?.text = name

    return cell
}

物品商店:

 import UIKit

 class ItemStore {

var allItems = ["Thanh", "David", "Tommy", "Maria"]


 }

AppDelegate:

  func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

    // Create ItemStore instance
    let itemStoreObject = ItemStore()
    let itemController = window!.rootViewController as! TestTableViewController
    itemController.itemstore = itemStoreObject

    return true
}

我收到以下错误:

 2017-06-16 21:30:33.046 TestTableViewSearch[15419:1672786] *** Assertion 
 failure in -[UITableView _configureCellForDisplay:forIndexPath:], 
 /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-
 3600.6.21/UITableView.m:8042
 2017-06-16 21:30:33.052 TestTableViewSearch[15419:1672786] *** Terminating 
 app due to uncaught exception 'NSInternalInconsistencyException', reason: 
 'UITableView (<UITableView: 0x7fa6e3817a00; frame = (0 64; 375 603); 
 clipsToBounds = YES; autoresize = RM+BM; gestureRecognizers = <NSArray: 
 0x600000058f60>; layer = <CALayer: 0x600000025680>; contentOffset: {0, 0}; 
 contentSize: {375, 176}>) failed to obtain a cell from its dataSource 
 (<TestTableViewSearch.TestTableViewController: 0x7fa6e2406df0>)'
 *** First throw call stack:
 (
0   CoreFoundation                      0x0000000110c39d4b __exceptionPreprocess + 171
1   libobjc.A.dylib                     0x000000010df4c21e objc_exception_throw + 48
2   CoreFoundation                      0x0000000110c3de42 +[NSException raise:format:arguments:] + 98
3   Foundation                          0x000000010dae166d -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 195
4   UIKit                               0x000000010e57d9cd -[UITableView _configureCellForDisplay:forIndexPath:] + 222
5   UIKit                               0x000000010e5895eb -[UITableView _createPreparedCellForGlobalRow:withIndexPath:willDisplay:] + 860
6   UIKit                               0x000000010e5897e2 -[UITableView _createPreparedCellForGlobalRow:willDisplay:] + 74
7   UIKit                               0x000000010e55d2b0 -[UITableView _updateVisibleCellsNow:isRecursive:] + 3295
8   UIKit                               0x000000010e592b64 -[UITableView _performWithCachedTraitCollection:] + 110
9   UIKit                               0x000000010e5793be -[UITableView layoutSubviews] + 222
10  UIKit                               0x000000010e4e0ab8 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 1237
11  QuartzCore                          0x0000000113a7fbf8 -[CALayer layoutSublayers] + 146
12  QuartzCore                          0x0000000113a73440 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 366
13  QuartzCore                          0x0000000113a732be _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
14  QuartzCore                          0x0000000113a01318 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 280
15  QuartzCore                          0x0000000113a2e3ff _ZN2CA11Transaction6commitEv + 475
16  QuartzCore                          0x0000000113a2ed6f _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 113
17  CoreFoundation                      0x0000000110bde267 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
18  CoreFoundation                      0x0000000110bde1d7 __CFRunLoopDoObservers + 391
19  CoreFoundation                      0x0000000110bc28a6 CFRunLoopRunSpecific + 454
20  UIKit                               0x000000010e415aea -[UIApplication _run] + 434
21  UIKit                               0x000000010e41bc68 UIApplicationMain + 159
22  TestTableViewSearch                 0x000000010d96299f main + 111
23  libdyld.dylib                       0x0000000111be968d start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

【问题讨论】:

    标签: ios swift


    【解决方案1】:

    错误信息

    未能从其数据源获取单元格

    表示您的代码未正确处理UITableViewDataSource。这可能是由于 UITableViewDataSource 未正确实现,或者在 xib 中分配 datasourcedelegate 属性时可能存在错误。

    错误:

    我注意到您的课程TestTableViewController,不符合协议UITableViewDataSource

    所以让你的类 TestTableViewController 符合协议 UITableViewDataSource 就像这样:

    class TestTableViewController: UIViewController, UITableViewDelegate, UISearchBarDelegate , UITableViewDataSource 
    

    并在TestTableViewController 中定义UITableViewDataSource 方法。

    【讨论】:

    • 实际上该图像显示了 TestTableViewController 的出口,因此显示了从 My Table View 到 TestTableViewController 的数据源和委托出口。从堆栈跟踪我们可以看到 dataSource 是 TestTableViewController 的一个实例
    • @Jota Ahh 我明白了.. 我一定是匆忙错过了它。感谢您指出了这一点。我已更新我的答案以适应您建议的更改。
    【解决方案2】:

    错误信息显示:

    未能从其数据源中获取单元格

    您的视图控制器是:

    class TestTableViewController: UIViewController, UITableViewDelegate, UISearchBarDelegate{

    似乎缺少UITableViewDataSource

    【讨论】:

      【解决方案3】:

      我们可以在堆栈跟踪中看到实际的错误:

      Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView (...) failed to obtain a cell from its dataSource (&lt;TestTableViewSearch.TestTableViewController: 0x7fa6e2406df0&gt;)'"

      这意味着您的 cellForRowAtIndexPath 方法返回了一个 nil 单元格。您需要为情节提要中的单元格指定一个重用标识符并像这样初始化它:

      let cell = tableView.dequeueReusableCell(withReuseIdentifier: "YourReuseIdentifier", for: indexPath)
      

      同样在堆栈跟踪中我们可以看到数据源实际上是正确的:

      (...) dataSource  (<TestTableViewSearch.TestTableViewController: 0x7fa6e2406df0>)'"
      

      所以你只需要返回一个有效的单元格就可以了。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多