在某些情况下: tableView有多个类型的UItableViewCell,这些单元格实现了"setData(_:)“函数。所以,在
"tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell". 我可以使用"perfrom(selector:)"来避免“快速类型转换”(如
if indexPath.row == 1{ cell as! SomeCoustomCell })". --但是如果我使用"#selector()",我仍然需要知道单元格的类型并使用"SomeCoustomCell.setData(_:)". --因此,我用"cell.perform(Selector("setData:"), with: dataSource[indexPath.row]["data"])"来解决它,尽管它有一个警告。

斯威夫特的正确方式是什么??
发布于 2017-08-07 07:18:26
使用perfrom(selector:)不是最好的方法。您可以使用协议。有点像
protocol Configurable{
func setData(_ data: String)
}您的所有自定义tableView单元都应符合此协议。然后在cellForRowAtIndexpath
if cell is Configurable{
cell.setData(data)
}https://stackoverflow.com/questions/45540330
复制相似问题