Ios Xcode 9 Swift 4 UITableView无法识别的选择器已发送到实例

Ios Xcode 9 Swift 4 UITableView无法识别的选择器已发送到实例,ios,swift,uitableview,Ios,Swift,Uitableview,我刚从obj-c搬到swift,我在UIViewController中遇到了UITableView问题 我已将Interface Builder中的委托和数据源从TableView设置为View控制器,但效果不佳 这是截图 这是我的视图控制器 class SideMenuViewController: UIViewController { @IBOutlet var tableView: UITableView! var cellRow = ["Item 1","Item 2","Item

我刚从obj-c搬到swift,我在UIViewController中遇到了UITableView问题

我已将Interface Builder中的委托和数据源从TableView设置为View控制器,但效果不佳

这是截图

这是我的视图控制器

class SideMenuViewController: UIViewController {

@IBOutlet var tableView: UITableView!
var cellRow = ["Item 1","Item 2","Item 3"]

 // MARK: - TableView Delegates
func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

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

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)

    let object = cellRow[indexPath.row]
    cell.textLabel!.text = object.description
    return cell
}
这里是错误

reason: '-[MyApp.SideMenuViewController tableView:numberOfRowsInSection:]: unrecognized selector sent to instance
但是,如果我使用视图控制器中的代码手动设置委托和数据源,它将正常工作。是虫子吗


谢谢

您必须添加
UITableViewDelegate
UITableViewDataSource
。委托类必须告诉编译器我们正在采用协议,然后实现委托方法

另一个好看的方法是实现协议,委托是通过使用扩展实现的

class SideMenuViewController: UIViewController {
    // ..        
}

extension SideMenuViewController : UITableViewDelegate {
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)

        let object = cellRow[indexPath.row]
        cell.textLabel!.text = object.description
        return cell
    }
}

extension SideMenuViewController : UITableViewDataSource {
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return cellRow.count
    }
}
斯威夫特4 您可以像这样添加委托

步骤1:
类视图控制器:VVBaseViewController、UITableViewDelegate、UITableViewDataSource

步骤2实施委托和数据源方法

// MARK: - UITableViewDelegate, UITableViewDataSource
func numberOfSections(in tableView: UITableView) -> Int {
    return 20 // Here is 20 section in tableview and 1 row each section.
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 1 // Only 1 row in each section
}


func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    // Normal Cell (UITableViewCell)
    //        let cell:UITableViewCell = (tblProperty!.dequeueReusableCell(withIdentifier: cellReuseIdentifier) as UITableViewCell?)!
    
    // Custom Cell
    let cell:CellSubService = tblSubServices.dequeueReusableCell(withIdentifier: cellReuseIdentifier) as! CellSubService
    
    cell.lblServiceName.text = "Test Service"
    cell.lblRate.text = "$5 / Sq.Ft"
    
    return cell
}

我猜你忘了在
UIViewController
之后添加
UITableViewDelegate,UITableViewDataSource
。当使用obj-c时,我不必在视图控制器中手动添加它们,只需从Interface Builder中添加即可set@Alvin对。但是这是Swift,所以这不是真正相关的。嗨,看起来必须在视图控制器中包含这两个。因为在obj-c中,我们可以从IB设置它,而不需要在视图控制器中添加这两个。我错了吗?是的,你是对的,它在obj-c中有效,但在swift中你提到了它。非常感谢@还有一种快速的方法是使用扩展。请参阅更新的答案。
// MARK: - UITableViewDelegate, UITableViewDataSource
func numberOfSections(in tableView: UITableView) -> Int {
    return 20 // Here is 20 section in tableview and 1 row each section.
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 1 // Only 1 row in each section
}


func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    // Normal Cell (UITableViewCell)
    //        let cell:UITableViewCell = (tblProperty!.dequeueReusableCell(withIdentifier: cellReuseIdentifier) as UITableViewCell?)!
    
    // Custom Cell
    let cell:CellSubService = tblSubServices.dequeueReusableCell(withIdentifier: cellReuseIdentifier) as! CellSubService
    
    cell.lblServiceName.text = "Test Service"
    cell.lblRate.text = "$5 / Sq.Ft"
    
    return cell
}