在某些情况下: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"])"
虽然它有一个警告。
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/