gpt4 book ai didi

Swift - UITableViewCell?不可转换为 UITableViewCell

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

我是 Swift 开发新手,正在学习许多在线教程。这些教程中的大多数都引用了 Xcode 上的旧版本,并且代码会导致错误。谁能解释为什么下面的代码会产生“UITableViewCell”?无法转换为 UITableViewCella 以及我如何解决这个问题。

我使用的是 Xcode 7。

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

let cell = tableView.dequeueReusableCellWithIdentifier("customcell") as UITableViewCell

cell.textLabel?.text = array[indexPath.item]
return cell
}

最佳答案

有两种方法

1)

func dequeueReusableCellWithIdentifier(_ identifier: String) -> UITableViewCell?

它返回一个可选的,所以你必须解开可选的

let cell = tableView.dequeueReusableCellWithIdentifier("custom cell")!
<小时/>

另一种方法更好

2)

func dequeueReusableCellWithIdentifier(_ identifier: String,
forIndexPath indexPath: NSIndexPath) -> UITableViewCell

因为它返回一个非可选

let cell = tableView.dequeueReusableCellWithIdentifier("custom cell", forIndexPath:indexPath)

关于Swift - UITableViewCell?不可转换为 UITableViewCell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35258677/

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