ios - 我如何使用 dynamicType 在 swift3 中执行 "perform(selector:)"

标签 ios uitableview dynamictype

在某些情况下:tableView 有不止一种类型的 UItableViewCell,并且这些单元格实现了函数“setData(_:)”。 所以,在

"tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell".     

我可以用 “perfrom(选择器:)” 避免“快速类型转换(如
if indexPath.row == 1{ 单元格为!一些CoustomCell })". 但是如果我使用 “#选择器()” 我仍然需要知道电池的类型并使用 “SomeCoustomCell.setData(_:)”。 因此,我通过使用解决了它 "cell.perform(Selector("setData:"), with: dataSource[indexPath.row]["data"])" 虽然它有一个警告。

enter image description here swift 中的正确方法是什么?

最佳答案

使用 perfrom(selector:) 不是最好的方法。您可以使用协议(protocol)。有点像

protocol Configurable{
    func setData(_ data: String)
}

您所有的自定义 tableView 单元格都应符合此协议(protocol)。然后在 cellForRowAtIndexpath

if cell is Configurable{
    cell.setData(data)
}

关于ios - 我如何使用 dynamicType 在 swift3 中执行 "perform(selector:)",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45540330/

相关文章:

ios - 未找到框架 GoogleMapsCore

ios - 我在解析 XML 时显示一条错误消息

ios - 如何在 ios 中单击时更改 tableView Cell 按钮图像

iOS Swift 总结 CoreData 属性值

c# - 重构以使用具有不同输入对象的通用函数

iphone - iOS打印图像和文本

iOS如何计算标签的高度?

ios - didSelectRowAt 未触发(Swift 5)

Java参数具有不同的静态和动态类型?

swift - dynamicType + cast to protocol 导致崩溃