gpt4 book ai didi

uitableview - 类型 View Controller 不符合 UITableViewDataSource 协议(protocol)

转载 作者:行者123 更新时间:2023-11-30 10:21:36 25 4
gpt4 key购买 nike

我已经阅读了有关此主题的其他问题,代码看起来不错。大部头书?关于错误是什么的任何想法吗?

import UIKit

class ViewController: UIViewController, UITableViewDelegate,UITableViewDataSource
{



@IBOutlet weak var myTableView: UITableView!

var arrayOfPersons: [Person] = [Person]()

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

self.setUpPersons()

// self.myTableView.delegate = self
// self.myTableView.dataSource = self

}

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

func setUpPersons()
{
var person1 = Person(name: "Bill", number: 60, imageName: "bill-gates.jpg")

var person2 = Person(name: "Sara", number: 39, imageName: "sara-evans.jpg")

arrayOfPersons.append(person1)
arrayOfPersons.append(person2)


}

func tableView(tableView:UITableView!, numberOfRowsInSection section: Int)->Int
{
return arrayOfPersons.count

}

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

let cell: CustomCell = tableView.dequeueReusableCellWithIdentifier("cell")as CustomCell



if indexPath.row % 2 == 0
{
cell.backgroundColor = UIColor.purpleColor()
}
else{
cell.backgroundColor=UIColor.orangeColor()

}

let person = arrayOfPersons[indexPath.row]


return cell
}


}
}

最佳答案

您正在覆盖错误的签名。将您的函数替换为以下函数。不需要隐式可选并添加覆盖

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int 
{
return arrayOfPersons.count

}



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

let cell: CustomCell = tableView.dequeueReusableCellWithIdentifier("cell")as CustomCell



if indexPath.row % 2 == 0
{
cell.backgroundColor = UIColor.purpleColor()
}
else{
cell.backgroundColor=UIColor.orangeColor()

}

let person = arrayOfPersons[indexPath.row]


return cell
}


}

关于uitableview - 类型 View Controller 不符合 UITableViewDataSource 协议(protocol),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26227840/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com