[英]type viewcontroller does not conform to protocol uitableviewdatasource
[英]Type 'ViewController' does not conform to protocol 'UITableViewDataSource'
開始快速練習。 在 singleViewController 我試圖制作一個UITableView
。 在故事板中,我設置了數據源和委托。 在這里我收到錯誤*'ViewController' 不符合協議 'UITableViewDataSource' *
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var table: UITableView!
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.
}
}
func numberOfSectionsInTableView(tableView: UITableView!) -> 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
}
您應該在最后一個}
之前實現所有必需的方法,但是您已經在 UIViewController 之外編寫了它們。 此外,您需要更改行數的功能。
建議的編輯
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var table: UITableView!
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.
}
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
}
}
嘗試刪除! 在你的功能上。 那對我有用
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
}
您需要實現UITableViewDataSource
所有必需方法才能消除該錯誤。
基本上......你錯過了:
func tableView(tableView:UITableView!, numberOfRowsInSection section:Int) -> Int {
//return XX
}
我遇到了同樣的問題,在 Objective-C 中事情會很容易解決,可能是因為我們現在更熟悉它,但在這種情況下,swift 是非常新的,因此,它的錯誤通知相當模糊。
在我實現基於 UITableView 的應用程序時,我遇到了這個問題。 我通過按下命令並單擊 UITableView 打開了 UITableView 的實現文件。 在實現文件中,我們可以清楚地看到兩個函數是強制實現的,
我來到這篇文章並開始把事情放在一起,同時牢記我對 Objective-C 編程的微薄知識。 錯誤的原因是表格視圖由兩個元素定義,首先是部分和部分中的行,其次是表格視圖單元格。 默認情況下,表視圖中至少有一個節,但我們需要關於節中行數的一致性。 其次,我們需要知道我們將在一個部分的特定行中呈現哪個單元格。 無論如何,即使我們使用默認的 UITableViewCell,我們仍然需要一個標識符來訪問它以設置它的子視圖或屬性。 我希望它有幫助,因為我自己對 Swift 很陌生,所以我會感謝一些軟批評:)
以下代碼在 iOS 8.1 上對我不起作用。 在 XCode 6.1.1 中。 此代碼有效:
import UIKit
class ViewController : UIViewController,UITableViewDelegate,UITableViewDataSource {
override func viewDidLoad() {
super.viewDidLoad()
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
//currently only a testing number
return 25
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
var cell:UITableViewCell=UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "mycell")
cell.textLabel?.text = "row#\(indexPath.row)"
cell.detailTextLabel?.text = "subtitle#\(indexPath.row)"
return cell
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
我剛剛在 Swift 中遇到了同樣的問題。
你應該實現類中 UITableViewDataSource 的所有功能,也就是說應該實現以下功能:
func numberOfSectionsInTableView(tableView: UITableView) -> Int{}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {}
我不確定缺少 numberOfSectionsInTableView 的功能是否適合您。 對我來說,我必須在我的課堂上意識到這一點。
只需添加這兩個方法即可解決該錯誤[XCode8 Swift3]
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
}
為最新的 Xcode 6.1.1 GM_Seed 取出 tableview 和 NSIndexpath 的所有選項
只是一個提高可讀性的建議,您可以使用擴展來分離您的協議,如下所示:
class ViewController: UIViewController {
// Your lifecycle code here
}
extension ViewController: UITableDataSource {
func tableView(tableView:UITableView!, numberOfRowsInSection section:Int) -> Int {
return 20
}
}
extension ViewController: UITableViewDelegate {
...
}
這是一個常見的警告,表示“您尚未實現協議所需的方法”
故事板上的視圖對象可能需要數據源。 例如,TableView 需要一個數據源,通常,視圖控制器充當一個。
所以表視圖期望 ViewController 包含返回表視圖的“必備”信息的方法。
表視圖需要知道節的數量,每個節的行數等。
除非數據源對象返回所有必需的信息,否則警告將持續存在。
根據新的 swift 3 文檔更改“UITableViewDataSource”協議所需方法的語法:
內部 func tableView(_ tableView: UITableView, numberOfRowsInSection 部分: Int) -> Int
內部 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
這在使用 swift 3 編譯器編譯時對我有用
Ankit的答案在 Xcode 8 for Swift 2.3 中對我有用。 這是新語法。
extension ViewController: UITableViewDataSource {
internal func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
//return 1
}
internal func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
//return cell
}
}
添加這些方法
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
}
在ViewController
類下復制下面的代碼並指定您想要的行數(在第一部分)並定義每個單元格的內容(在第二個函數中)
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1 //no. of rows in the section
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: <#T##UITableViewCellStyle#>, reuseIdentifier: <#T##String?#>)
}
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.
}
}
通過自動完成建議的一些編輯,來自@MB_iOSDeveloper 在 swift 3, Xcode 8.3.2 上的上述回答,此代碼對我有用:
class MenuViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
override func viewDidLoad() {
super.viewDidLoad()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
//currently only a testing number
return 25
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
var cell:UITableViewCell=UITableViewCell(style: UITableViewCellStyle.subtitle, reuseIdentifier: "mycell")
cell.textLabel?.text = "row#\(indexPath.row)"
cell.detailTextLabel?.text = "subtitle#\(indexPath.row)"
return cell
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
通常,協議具有可選和必需的方法。 例如, UISearchBarDelegate和UITableViewDelegate是您可以聲明符合協議而不實現任何方法的情況。 但它不適用於UITableViewDataSource。
在官方文檔中,Protocol "UITableViewDataSource" -> Symbols -> Configure a Table View,方法: func tableView(UITableView, cellForRowAt: IndexPath)
和func tableView(UITableView, numberOfRowsInSection: Int)
用粗體的Required關鍵字表示。
只需刪除 viewDidLoad() 和 Build 並添加 viewDidLoad() 一切正常
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.