从 UITableViewCell 到 UITableViewCell 的条件转换总是成功
Conditional cast from UITableViewCell to UITableViewCell always succeds
当我编译我的应用程序时,我收到警告并且我不知道如何修复或隐藏它。从 UITableViewCell 到 UITableViewCell 的条件转换总是成功
这是我的代码部分,出现警告的地方。
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return arrayPDF.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "MyCellPDF", for: indexPath)
as? UITableViewCell else { return UITableViewCell() }
cell.textLabel?.text = arrayPDF[indexPath.row] + ".pdf"
return cell
}
我的这部分代码生成了警告。
as? UITableViewCell else { return UITableViewCell() }
一切正常。我卡住了。
提前致谢!
默认情况下,tableView.dequeueReusableCell(withIdentifier: "MyCellPDF", for: indexPath)
returns 一个 UITableViewCell
对象。
所以,当你这样做时:
guard let cell = tableView.dequeueReusableCell(withIdentifier: "MyCellPDF", for: indexPath) as? UITableViewCell else { return UITableViewCell() }
您正在将 UITableViewCell
对象转换为 UITableViewCell
。 Xcode 只是告诉你没用。
如果您的 class 是 UITableViewCell
,您可以使用:
let cell = tableView.dequeueReusableCell(withIdentifier: "MyCellPDF", for: indexPath)
如果是自定义 class,你可以使用守卫,但将 as? UITableViewCell
部分替换为 as? MyCustomTableViewCellClass
。
此致,
IMACODE
你用了guard .. else{},所以它必须是可选的,我猜你需要的功能是第一个而不是最后一个:
打开 func dequeueReusableCell(withIdentifier identifier: String) -> UITableViewCell? // 委托用于获取已分配的单元格,代替分配新的单元格。
@available(iOS 6.0, *)
open func dequeueReusableCell(withIdentifier identifier: String, for indexPath: IndexPath) -> UITableViewCell // newer dequeue method guarantees a cell is returned and resized properly,
您可以通过以下方式与任何一个权利:
let cell = tableView.dequeueReusableCell(withIdentifier: "MyCellPDF", for: indexPath)
或
guard let cell = tableView.dequeueReusableCell(withIdentifier: "MyCellPDF") else { return UITableViewCell() }
当我编译我的应用程序时,我收到警告并且我不知道如何修复或隐藏它。从 UITableViewCell 到 UITableViewCell 的条件转换总是成功
这是我的代码部分,出现警告的地方。
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return arrayPDF.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "MyCellPDF", for: indexPath)
as? UITableViewCell else { return UITableViewCell() }
cell.textLabel?.text = arrayPDF[indexPath.row] + ".pdf"
return cell
}
我的这部分代码生成了警告。
as? UITableViewCell else { return UITableViewCell() }
一切正常。我卡住了。
提前致谢!
默认情况下,tableView.dequeueReusableCell(withIdentifier: "MyCellPDF", for: indexPath)
returns 一个 UITableViewCell
对象。
所以,当你这样做时:
guard let cell = tableView.dequeueReusableCell(withIdentifier: "MyCellPDF", for: indexPath) as? UITableViewCell else { return UITableViewCell() }
您正在将 UITableViewCell
对象转换为 UITableViewCell
。 Xcode 只是告诉你没用。
如果您的 class 是 UITableViewCell
,您可以使用:
let cell = tableView.dequeueReusableCell(withIdentifier: "MyCellPDF", for: indexPath)
如果是自定义 class,你可以使用守卫,但将 as? UITableViewCell
部分替换为 as? MyCustomTableViewCellClass
。
此致,
IMACODE
你用了guard .. else{},所以它必须是可选的,我猜你需要的功能是第一个而不是最后一个: 打开 func dequeueReusableCell(withIdentifier identifier: String) -> UITableViewCell? // 委托用于获取已分配的单元格,代替分配新的单元格。
@available(iOS 6.0, *)
open func dequeueReusableCell(withIdentifier identifier: String, for indexPath: IndexPath) -> UITableViewCell // newer dequeue method guarantees a cell is returned and resized properly,
您可以通过以下方式与任何一个权利:
let cell = tableView.dequeueReusableCell(withIdentifier: "MyCellPDF", for: indexPath)
或
guard let cell = tableView.dequeueReusableCell(withIdentifier: "MyCellPDF") else { return UITableViewCell() }