Swift 当我编写UITableViewDataSource时,我看到错误

Swift 当我编写UITableViewDataSource时,我看到错误,swift,Swift,这是我和斯威夫特之间的问题 此错误类型viewcontroller不符合协议 UITableViewDataSource 为什么 如果不添加协议所需的功能:cellForRowAt、numberOfSections和numberofrowsinssection(Swift 3版本),就不能将UITableViewDataSource放在视图控制器上。请阅读以下文章,了解如何实现基本的UITableView: 尝试添加这两种方法: func tableView(tableView: UITable

这是我和斯威夫特之间的问题

此错误类型viewcontroller不符合协议 UITableViewDataSource

为什么


如果不添加协议所需的功能:
cellForRowAt
numberOfSections
numberofrowsinssection
(Swift 3版本),就不能将
UITableViewDataSource
放在视图控制器上。请阅读以下文章,了解如何实现基本的
UITableView

尝试添加这两种方法:

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

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")
    return cell
}

我会将此模板用于UITableView,然后在中添加额外的功能

import UIKit

class TableViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

override func viewDidLoad() {
    super.viewDidLoad()

    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = false

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem()
}

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

// MARK: - Table view data source

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return 0
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of rows
    return 0
}


func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath)

    // Configure the cell...

    return cell
}
}

协议
UITableViewDataSource
需要几种方法

您可以轻松找到缺失的内容并进行修复:

  • 压榨⌘B构建代码
  • 压榨⌘4以显示问题导航器
  • 单击问题行前面的披露三角形
  • 实现显示的所需方法
import UIKit

class TableViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

override func viewDidLoad() {
    super.viewDidLoad()

    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = false

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem()
}

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

// MARK: - Table view data source

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return 0
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of rows
    return 0
}


func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath)

    // Configure the cell...

    return cell
}
}