gpt4 book ai didi

ios - 在 Swift 上创建 UITableView 单元格

转载 作者:搜寻专家 更新时间:2023-10-30 21:49:19 24 4
gpt4 key购买 nike

我正在尝试在我的委托(delegate)方法 cellForRowAtIndexPath 上使用 swift 创建 UITableViewCell,

代码很简单,就像在 Objective-c 中一样,只是试图将语言变形为 swift。

我在这一行遇到错误

var cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as UITableViewCell

if(cell == nil)
{
let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle,reuseIdentifier:cellIdentifier)
}

错误是 UITableViewCell 不能转换为“MirrorDisposition”

我查了例子,代码是这样的

if !cell
{let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle,reuseIdentifier:cellIdentifier)
}

但它也会报错。

我做错了什么?

最佳答案

截至最新的测试版(测试版 6)不能将非可选类型与 nil 进行比较

因此,您必须将您的单元格 Var 声明为可选。

像这样的东西会正常工作(在我的脑海中 - 我面前没有 Xcode):

//declare a tableViewCell as an implicitly unwrapped optional...
var cell:UITableViewCell! = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as? UITableViewCell

//you CAN check this against nil, if nil then create a cell (don't redeclare like you were doing...
if(cell == nil)
{
cell = UITableViewCell(style: UITableViewCellStyle.Subtitle,reuseIdentifier:cellIdentifier)
}

关于ios - 在 Swift 上创建 UITableView 单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25399193/

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