编辑:有人建议这是另一个问题的副本。我没有实施:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section //(in swift of course).
在ViewController中,我以编程方式创建一个tableView并将其添加为子视图。我有一个名为DataSource的类,它通过两个扩展来采用UITableViewDelegate和UITableViewDataSource。
我按照给定here
的示例我收到错误:
ViewController tableView:numberOfRowsInSection:]:无法识别的选择器发送到实例0x7f88cc406ac0'
0x7f88cc406ac0是包含ViewController的地址。
即使我通过扩展使包含的ViewController采用UITableViewDelegate然后调用tableView的数据源实现:numberOfRowsInSection我仍然看到错误。
想法?
import UIKit
class GenericDataSource: NSObject {
let identifier = "cell"
var array: [Any] = ["Dogs","Cats","Mice"]
func registerCells(forTableView tableView: UITableView) {
tableView.register(UITableViewCell.self, forCellReuseIdentifier: identifier)
// tableView.register(UINib(nibName: "", bundle: nil), forCellReuseIdentifier: identifier)
}
func loadCell(atIndexPath indexPath: IndexPath, forTableView tableView: UITableView) -> UITableViewCell {
// let cell = tableView.dequeueReusableCell(withIdentifier: identifier, for: indexPath)
let cell:UITableViewCell=UITableViewCell(style: UITableViewCellStyle.subtitle, reuseIdentifier: "cell")
cell.textLabel!.text = array [indexPath.row] as? String
return cell
}
}
// UITableViewDataSource
extension GenericDataSource: UITableViewDataSource {
func numberOfSections(in tableView: UITableView) -> Int {
return 0
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return array.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
return self.loadCell(atIndexPath: indexPath, forTableView: tableView)
}
}
// UITableViewDelegate
extension GenericDataSource: UITableViewDelegate {
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
}
}
protocol GenericDataSourceDelegate: class {
// Delegate callbacks methods
}
extension ViewController: UITableViewDelegate {
func numberOfSections(in tableView: UITableView) -> Int {
return self.dataSource.numberOfSections(in: tableView)
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return dataSource.tableView(tableView,numberOfRowsInSection:section)
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
return dataSource.tableView(tableView,cellForRowAt:indexPath)
}
}
func requestTableView() -> UITableView {
return UITableView(frame: UIScreen.main.bounds, style: UITableViewStyle.plain)
}
class ViewController: UIViewController {
var tableView: UITableView?
var dataSource = GenericDataSource()
override func viewDidLoad() {
super.viewDidLoad()
tableView = requestTableView()
if(tableView != nil) {
self.tableView!.delegate = self
self.tableView!.dataSource = dataSource
self.view.addSubview(self.tableView!)
}
}
}
这里要求的是整个错误消息:
2018-03-09 08:37:52.479763-0800 Job [3560:6187366] - [Job.ViewController tableView:numberOfRowsInSection:]:无法识别的选择器发送到实例0x7f9164c065b0 2018-03-09 08:37:52.623521-0800工作[3560:6187366] *由于未捕获的异常终止应用程序' NSInvalidArgumentException',原因:' - [Job.ViewController tableView: numberOfRowsInSection:]:无法识别的选择器发送到实例0x7f9164c065b0' * 第一次抛出调用堆栈: ( 0 CoreFoundation 0x00000001089fc12b exceptionPreprocess + 171 1 libobjc.A.dylib 0x0000000104d08f41 objc_exception_throw + 48 2 CoreFoundation 0x0000000108a7d024 - [NSObject(NSObject)doesNotRecognizeSelector:] + 132 3 UIKit 0x00000001057d8f51 - [UIResponder doesNotRecognizeSelector:] + 295 4 CoreFoundation 0x000000010897ef78 ___ forwarding _ + 1432 5 CoreFoundation 0x000000010897e958 _CF_forwarding_prep_0 + 120 6 UIKit 0x0000000105713b4c - [UITableView _numberOfRowsInSection:] + 62 7 UIKit 0x00000001059c60e1 - [UISectionRowData refreshWithSection:tableView:tableViewRowData:] + 2389 8 UIKit 0x00000001059cc5ab - [UITableViewRowData rectForFooterInSection:heightCanBeGuessed:] + 487 9 UIKit 0x00000001059cc711 - [UITableViewRowData heightForTable] + 61 10 UIKit 0x00000001056c1a65 - [UITableView _updateContentSize] + 372 11 UIKit 0x00000001056f267d - [UITableView _rebuildGeometry] + 66 12 UIKit 0x00000001056f003c - [UITableView didMoveToWindow] + 145 13 UIKit 0x000000010566d147 - [UIView(内部)_didMoveFromWindow:toWindow:] + 1748 14 UIKit 0x0000000105686a81 - [UIScrollView _didMoveFromWindow:toWindow:] + 84 15 UIKit 0x000000010565f1e1 45- [UIView(Hierarchy)_postMovedFromSuperview:] _ block_invoke + 151 16 UIKit 0x000000010565f0c8 - [UIView(Hierarchy)_postMovedFromSuperview:] + 828 17 UIKit 0x000000010566fcbd - [UIView(内部)_addSubview:定位:relativeTo:] + 1973 18 UIKit 0x0000000105618aa2 - [UIWindow addRootViewControllerViewIfPossible] + 845 19 UIKit 0x0000000105618ed7 - [UIWindow _setHidden:forced:] + 294 20 UIKit 0x000000010562be54 - [UIWindow makeKeyAndVisible] + 42 21 UIKit 0x000000010559e8b8 - [UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 4737 22 UIKit 0x00000001055a3aeb - [UIApplication _runWithMainScene:transitionContext:completion:] + 1720 23 UIKit 0x000000010596d6f8 __111 - [__ UICanvasLifecycleMonitor_Compatability _scheduleFirstCommitForScene:transition:firstActivation:completion:] _ block_invoke + 924 24 UIKit 0x0000000105d434c8 + [_ UICanvas _enqueuePostSettingUpdateTransactionBlock:] + 153 25 UIKit 0x000000010596d2f1 - [__ UICanvasLifecycleMonitor_Compatability _scheduleFirstCommitForScene:transition:firstActivation:completion:] + 249 26 UIKit 0x000000010596db6b - [__ UICanvasLifecycleMonitor_Compatability activateEventsOnly:withContext:completion:] + 696 27 UIKit 0x00000001062eba69 __82 - [_ UIApplicationCanvas _transitionLifecycleStateWithTransitionContext:completion:] _ block_invoke + 262 28 UIKit 0x00000001062eb922 - [_ UIApplicationCanvas _transitionLifecycleStateWithTransitionContext:completion:] + 444 29 UIKit 0x0000000105fc89c8 __125 - [_ UICanvasLifecycleSettingsDiffAction performActionsForCanvas:withUpdatedScene:settingsDiff:fromSettings:transitionContext:] _ block_invoke + 221 30 UIKit 0x00000001061c7b06 _performActionsWithDelayForTransitionContext + 100 31 UIKit 0x0000000105fc888b - [_ UICanvasLifecycleSettingsDiffAction performActionsForCanvas:withUpdatedScene:settingsDiff:fromSettings:transitionContext:] + 231 32 UIKit 0x0000000105d42b25 - [_ UICanvas场景:didUpdateWithDiff:transitionContext:完成:] + 392 33 UIKit 0x00000001055a236a - [UIApplication workspace:didCreateScene:withTransitionContext:completion:] + 523 34 UIKit 0x0000000105b7d605 - [UIApplicationSceneClientAgent场景:didInitializeWithEvent:完成:] + 369 35 FrontBoardServices 0x000000010a8bfcc0 - [FBSSceneImpl _didCreateWithTransitionContext:completion:] + 338 36 FrontBoardServices 0x000000010a8c87b5 __56- [FBSWorkspace客户端:handleCreateScene:withCompletion:] _ block_invoke_2 + 235 37 libdispatch.dylib 0x0000000109b1e33d _dispatch_client_callout + 8 38 libdispatch.dylib 0x0000000109b239f3 _dispatch_block_invoke_direct + 592 39 FrontBoardServices 0x000000010a8f4498 __FBSSERIALQUEUE_IS_CALLING_OUT_TO_A_BLOCK + 24 40 FrontBoardServices 0x000000010a8f414e - [FBSSerialQueue _performNext] + 464 41 FrontBoardServices 0x000000010a8f46bd - [FBSSerialQueue _performNextFromRunLoopSource] + 45 42 CoreFoundation 0x000000010899f101 CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION + 17 43 CoreFoundation 0x0000000108a3ef71 __CFRunLoopDoSource0 + 81 44 CoreFoundation 0x0000000108983a19 __CFRunLoopDoSources0 + 185 45 CoreFoundation 0x0000000108982fff __CFRunLoopRun + 1279 46 CoreFoundation 0x0000000108982889 CFRunLoopRunSpecific + 409 47 GraphicsServices 0x000000010b1889c6 GSEventRunModal + 62 48 UIKit 0x00000001055a55d6 UIApplicationMain + 159 49 Jobr 0x00000001043ee5e7 main + 55 50 libdyld.dylib 0x0000000109b9ad81 start + 1 51 ??? 0x0000000000000001 0x0 + 1 ) libc ++ abi.dylib:以NSException类型的未捕获异常终止 (lldb)
答案 0 :(得分:0)
在Apple Documentation中,tableView numberOfRowsInSection
方法是数据源方法,而不是委托方法。
因此,您需要创建一个新扩展并在其中移动numberOfRowsInSection
方法:
extension ViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return dataSource.tableView(tableView,numberOfRowsInSection:section)
}
}