ios - ViewController 不确认协议(protocol) UITableViewDataSource

标签 ios uitableview swift

我做的很好但仍然显示错误“ViewController does not confirm to protocol” 我在这个网站上搜索了很多,我发现了同样的问题,但没有找到任何正确答案 我已经将 UITableView 的连接添加到 UITableViewDelegate 和 UITableViewDataSource,但仍然显示错误我该怎么办

这是我的代码

class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

@IBOutlet var tableView: UITableView!


func tableView(tableView:UITableView!, numberOfRowsInSection section:Int) -> Int
{
    return 20
}

func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell!
{

    let cell:UITableViewCell=UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "mycell")
    cell.textLabel.text="row#\(indexPath.row)"
    cell.detailTextLabel.text="subtitle#\(indexPath.row)"

    return cell

}



}

最佳答案

请注意删除委托(delegate)方法中的 !,这是错误的。

class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

@IBOutlet var tableView: UITableView!


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

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{

    let cell:UITableViewCell=UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "mycell")
    cell.textLabel.text="row#\(indexPath.row)"
    cell.detailTextLabel.text="subtitle#\(indexPath.row)"

    return cell

}



}

关于ios - ViewController 不确认协议(protocol) UITableViewDataSource,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26255099/

相关文章:

ios - 根据swift 4中排序的文本安排其余两个标签

ios - 查看 AVPlayer 是否正在从互联网加载或播放 mp3

ios - 如何从 iOS 使用 yammer 的 oauth2?

ios - 如何多次使用audioPlayerDidFinishPlaying

ios - 如何进行 NSDecimal 数学运算?

ios - UIScrollView 内部一致性崩溃

objective-c - 是否可以刷新 UITableView 中的单个 UITableViewCell?

ios - 隐藏 separatorColor 直到 UITableView 有内容

ios - 当 TouchMoved 函数上的 x 坐标发生变化时,动态更改 UICollection View 的宽度

swift - Swift 中常见的父 View