繁体   English   中英

类型TypeViewOne不符合协议UITableViewDataSource

[英]type TypeViewOne does not conform to protocol UITableViewDataSource

这是我的代码

class TableViewOne: UITableViewDataSource, UITableViewDelegate {


     func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("firstCell") as! firstCell
        let index = indexPath.row
        cell.labelMy.text = ViewController.firstData![index]
        return cell

    }

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

     func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if let s = ViewController.firstData {
            return s.count
        }
        return 0
    }

}

我不知道为什么这次出现这个错误,尽管当我使用UITableViewController的子类时,这些就足够了,听起来像UITableViewControlelr已经为其他必需的函数实现了,但是我不知道它们是我自己。

UITableViewDataSource要求您遵循NSObject协议,而TableViewOne (纯swift类)则不遵循。 更改声明以从NSObject继承,如下所示:

class TableViewOne: NSObject, UITableViewDataSource, UITableViewDelegate

其次,您不需要override关键字,因为TableViewOne不会将另一个对象作为子类。 UITableViewController的默认实现为返回1,但由于您没有将UITableViewController为子类,因此无法重写任何方法。

override func numberOfSectionsInTableView(tableView: UITableView) -> Int

func numberOfSectionsInTableView(tableView: UITableView) -> Int

确保将datasourcestoryboard xib storyboardxibdelegate给您的班级。 像这样:

在此处输入图片说明

Datasource将使您的类符合: cellForRowAtIndexPathnumberOfSectionnumberRowInSection

看来您已经在情节提要中使用了UIViewController并向其中添加了UITableView,如果是这样,则子类化UIViewController并像这样更改您的类

class TableViewOne: UIViewController,UITableViewDataSource, UITableViewDelegate {

如果您在情节提要中使用了TableViewController,则仅需要子类化UITableViewController

class TableViewOne: UITableViewController{

&您只需要添加以下功能即可确认tableview数据源

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {

    return 0
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return 0
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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