简体   繁体   中英

UITableView inside UIView in Swift 3.0

I have a problem when put a UITableView into UIView .

I set a delegate and datasource and implement all methods.

This is my UIView Class.

@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...")
   } 
}

And this is the outles

在此处输入图片说明

When run my app. I get this error

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

Log img.

在此处输入图片说明

Any idea...

Thanks.

Connect IB of dataSource and delegate to ChooserView instead of any other view.

Just right click on your table view and Follow the attached gif for connecting IB properly :

在此处输入图片说明

首先,您已经从情节提要中应用了插座,并且也在代码中进行了指定。这是没有必要的;其次,您的表中是否有包含标签或按钮的自定义类?如果您错过了与IBoutlet的任何连接,则通常会发生此问题情节提要或同一插座的指定2个连接检查一次。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM