gpt4 book ai didi

ios - 无法将 UITableViewDataSource 与自定义单元格一起使用

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

如果我使用自定义单元格 BaseCell,则无法导入 UITableViewDataSource。仅适用于 UITableViewCell 尽管 BaseCell 来自类型 UITableViewCell

import UIKit

class XptoViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

@IBOutlet weak var table: UITableView!

override func viewDidLoad() {
super.viewDidLoad()

// Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(_ tableView:UITableView, numberOfRowsInSection section:Int) -> Int {
return 10
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> BaseCell {
let cell = BaseCell()
return cell
}

最佳答案

尝试相反的方法:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = BaseCell()
return cell
}

但你可能想使用类似的东西:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("BaseCell") as? BaseCell
return cell!
}

确保将“BaseCell”设置为单元标识符,如下所示: Sample of interface Builder

如果您遇到运行时错误,例如“展开可选时发现 nil”,您需要将 BaseCell 视为您的单元格的类,请参见图片: enter image description here

关于ios - 无法将 UITableViewDataSource 与自定义单元格一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43326701/

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