gpt4 book ai didi

ios - ViewController 不符合协议(protocol) UITableViewDataSource

转载 作者:搜寻专家 更新时间:2023-11-01 07:22:24 25 4
gpt4 key购买 nike

我最近遇到了一个问题。

enter image description here

我也会发布一些代码。尝试了网上的方法,但没有成功。此外,我已将 2 个表链接到 View Controller 。

enter image description here

这是一些代码。

class ViewController2: UIViewController, UITableViewDataSource, UITableViewDelegate {

@IBOutlet weak var carImageView: UIImageView!
@IBOutlet weak var pieseDorite: UITableView!
@IBOutlet weak var pieseDisponibile: UITableView!


var fullNameArr : [String] = [] // i populate the string in a action button, no problem with the arrays
var fullNameArrDorit : [String] = []

override func viewDidLoad()
{
super.viewDidLoad()
carImageView.image = UIImage(named: ("simplu"))
carImageView.contentMode = .ScaleAspectFit

pieseDisponibile.delegate = self
pieseDisponibile.dataSource = self
self.view.addSubview(pieseDisponibile)

pieseDorite.delegate = self
pieseDorite.dataSource = self
self.view.addSubview(pieseDorite)
}


func tableView(pieseDisponibile: UITableView, numberOfRowsInSection section:Int) -> Int
{
return fullNameArr.count
}

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

let cell:UITableViewCell=UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "cell")
cell.textLabel!.text = String(fullNameArr[indexPath.row])
return cell
}

func tableView2(pieseDorite: UITableView, numberOfRowsInSection section:Int) -> Int
{
return fullNameArrDorit.count
}

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

let cell2:UITableViewCell=UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "cell2")
cell2.textLabel!.text = String(fullNameArrDorit[indexPath.row])
return cell2
}

}

另外,尝试添加所有其他功能,问题依然存在。我想我链接了一些不好的东西,我不知道。

最佳答案

请将所有tableView2替换为tableView,每个方法只保留1个。您需要为所有 UITableView 实例使用相同的委托(delegate)方法。您可以通过将第一个参数与您的 UITableView 属性进行比较来分隔此方法中的不同实例。还要添加节数方法:

override func viewDidLoad()
{
super.viewDidLoad()
carImageView.image = UIImage(named: "simplu")
carImageView.contentMode = .ScaleAspectFit

pieseDisponibile.delegate = self
pieseDisponibile.dataSource = self
view.addSubview(pieseDisponibile)

pieseDorite.delegate = self
pieseDorite.dataSource = self
view.addSubview(pieseDorite)
}

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

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
if tableView == pieseDisponibile {return fullNameArr.count}
return fullNameArrDorit.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
if tableView == pieseDisponibile {
let cell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "cell")
cell.textLabel?.text = String(fullNameArr[indexPath.row])
return cell
}
let cell2 = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "cell2")
cell2.textLabel?.text = String(fullNameArrDorit[indexPath.row])
return cell2
}

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

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