Reputation: 173
I have the following layout whereby I have my searchbar at the top and does not scroll with tableView. I have wired up the datasource and delegate for UITableView with my custom UIViewControler but I am not getting my tableview populated but instead I am getting the error below. Please can someone advise?
My Layout:
TestTableViewController - inspecter:
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
}
ItemStore:
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
}
I am getting the following error:
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)
Upvotes: 1
Views: 196
Reputation: 2115
The error message
failed to obtain a cell from its dataSource
means that your code is not handling UITableViewDataSource
properly. It might be due to UITableViewDataSource
not implemented correctly or there might be some error in assigning the datasource and delegate properties in the xib.
Mistake :
I notice that your class TestTableViewController
, does not conform to protocol UITableViewDataSource
So make your class TestTableViewController
conform to protocol UITableViewDataSource
like so :
class TestTableViewController: UIViewController, UITableViewDelegate, UISearchBarDelegate , UITableViewDataSource
and define UITableViewDataSource
methods in TestTableViewController
.
Upvotes: 2
Reputation: 149
We can see in the stack trace the actual error:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView (...) failed to obtain a cell from its dataSource (<TestTableViewSearch.TestTableViewController: 0x7fa6e2406df0>)'"
That means that your cellForRowAtIndexPath method returned a nil cell. You need to specify a reuse identifier for the cell in the Storyboard and init it like this:
let cell = tableView.dequeueReusableCell(withReuseIdentifier: "YourReuseIdentifier", for: indexPath)
Also in the stack trace we can see that the data source is actually correct:
(...) dataSource (<TestTableViewSearch.TestTableViewController: 0x7fa6e2406df0>)'"
So you only need to return a valid cell and it should work.
Upvotes: 0
Reputation: 77476
The error message shows:
failed to obtain a cell from its dataSource
Your view controller is:
class TestTableViewController: UIViewController, UITableViewDelegate, UISearchBarDelegate{
Which appears to be missing UITableViewDataSource
Upvotes: 2