【问题标题】:UITableView inside UIView in Swift 3.0Swift 3.0 中的 UIView 内的 UITableView
【发布时间】:2017-02-01 18:00:14
【问题描述】:

UITableView 放入 UIView 时出现问题。

我设置了 delegatedatasource 并实现所有方法。

这是我的 UIView 类。

@objc class ChooserView: UIView, UITableViewDelegate, UITableViewDataSource {


@IBOutlet weak var backgroundView: UIView!
@IBOutlet weak var showView: UIView!
@IBOutlet weak var tableView: UITableView!    

public class func createChooserView() -> ChooserView {

    var view: ChooserView!
    view = Bundle.main.loadNibNamed(String(describing: ChooserView.self), owner: self, options: nil)?.first as! ChooserView

    _ = MLAutolayouts.fillContainer(UIApplication.shared.keyWindow!.subviews.first!, view: view)

    //Register cell nib
    view.tableView.register(UINib(nibName: "PhoneCell", bundle: nil), forCellReuseIdentifier: "PhoneCell")

    return view
    }

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 1
    }

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: PhoneCell.self)) as! PhoneCell

    return cell
    }

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    print("TAP...")
   } 
}

这就是出口

当运行我的应用程序时。我收到此错误

   -[UIView tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x7fded2e772d0

记录图片。

任何想法...

谢谢。

【问题讨论】:

  • 您已经在代码中声明了您的类,但您似乎忘记声明您在 IB 中的视图具有您的类的类型,这就是为什么您看到 -[UIView tableView:numberOfRowsInSection:] 而不是 -[ChooserView ...
  • 如何连接UIView?? @哈希姆

标签: ios uitableview swift3


【解决方案1】:

连接数据源的 IB 并委托给 ChooserView 而不是任何其他视图。

只需右键单击您的表格视图,然后 按照随附的 gif 正确连接 IB:

【讨论】:

    【解决方案2】:

    首先,您已经应用了故事板中的插座,并且您也在代码中指定。没有必要。其次,您的表格是否有任何包含标签或按钮的自定义类?如果您错过了任何内容,通常会出现此问题IBoutlet 与 storyboard 的连接或指定的 2 个连接为同一个 outlet 检查一次。

    【讨论】:

    • 嗨 josueh 连接您的 uiview 的方式与您对表格视图所做的类似。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多