iosuitableviewdynamictype

How can i use dynamicType to do "perform(selector:)" in swift3


In some case: The tableView has more than one type of UItableViewCell and these cells implement the function "setData(_:)". So, in the

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

I can use "perfrom(selector:)" to avoid "swift type conversion (like
if indexPath.row == 1{ cell as! SomeCoustomCell })". But if i use "#selector()" i still need konw the type of cell and use "SomeCoustomCell.setData(_:)". Thus, i solved it by use "cell.perform(Selector("setData:"), with: dataSource[indexPath.row]["data"])" Although it has a warning.

enter image description here What's the correct way in swift??


Solution

  • Using perfrom(selector:) is not the best way. You could use a protocol. Something like

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

    All your custom tableView cells should conform to this protocol. Then in cellForRowAtIndexpath

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