Ios 使用泛型设置cellDelegate

Ios 使用泛型设置cellDelegate,ios,swift,uitableview,generics,swift3,Ios,Swift,Uitableview,Generics,Swift3,嗨,伙计们,你好吗?希望你们没事 我需要一点帮助,我提前谢谢你 我的自定义单元格中有一个自定义委托,如下所示: protocol customTableViewCellDelegate: NSObjectProtocol { func buttonPressed(customCell: customTableViewCell) } extension UITableView { func layoutTemplateCell<T: UIViewController>

嗨,伙计们,你好吗?希望你们没事

我需要一点帮助,我提前谢谢你

我的自定义单元格中有一个自定义委托,如下所示:

protocol customTableViewCellDelegate: NSObjectProtocol {
    func buttonPressed(customCell: customTableViewCell)
} 
extension UITableView {
    func layoutTemplateCell<T: UIViewController>(indexPath: IndexPath, viewController: T.Type) -> UITableViewCell {
            let cell = UITableViewCell()

                switch template {
                case customCell:
                    let cell = self.dequeueReusableCell(withIdentifier: customTableViewCell.identifier) as! customTableViewCell
                    cell.delegate = viewController.self
                    return cell
                default:
                    break
                }
            return cell
        }
}
我有一个扩展来在表视图中设置单元格,如下所示:

protocol customTableViewCellDelegate: NSObjectProtocol {
    func buttonPressed(customCell: customTableViewCell)
} 
extension UITableView {
    func layoutTemplateCell<T: UIViewController>(indexPath: IndexPath, viewController: T.Type) -> UITableViewCell {
            let cell = UITableViewCell()

                switch template {
                case customCell:
                    let cell = self.dequeueReusableCell(withIdentifier: customTableViewCell.identifier) as! customTableViewCell
                    cell.delegate = viewController.self
                    return cell
                default:
                    break
                }
            return cell
        }
}
扩展UITableView{ func layoutTemplateCell(indexPath:indexPath,viewController:T.Type)->UITableViewCell{ let cell=UITableViewCell() 开关模板{ 案例单元: 将cell=self.dequeueReusableCell(标识符为customTableViewCell.identifier)设为!customTableViewCell cell.delegate=viewController.self 返回单元 违约: 打破 } 返回单元 } } 但是我在cell.delegate“无法将T.type类型的值赋给customTableViewCellDelegate类型”中得到一个错误

我不知道如何正确使用泛型,也不知道如何修复此错误


我希望你们能帮助我。感谢您抽出时间阅读本文,祝您度过愉快的一天

您试图将视图控制器的类分配给委托属性,而不是视图控制器实例。你想要的是:

cell.delegate = viewController
我不明白你为什么要用泛型。您可以使用以下协议:

protocol CustomTableViewCellDelegate: NSObjectProtocol {
    func buttonPressed(customCell: UITableViewCell)
}

extension UITableView {
    func layoutTemplateCell(indexPath: IndexPath, viewController: CustomTableViewCellDelegate) -> UITableViewCell {
            let cell = UITableViewCell()

                switch template {
                case customCell:
                    let cell = self.dequeueReusableCell(withIdentifier: customTableViewCell.identifier) as! CustomTableViewCell
                    cell.delegate = viewController
                    return cell
                default:
                    break
                }
            return cell
        }
}

您正在尝试将视图控制器的类分配给委托属性,而不是视图控制器实例。你想要的是:

cell.delegate = viewController
我不明白你为什么要用泛型。您可以使用以下协议:

protocol CustomTableViewCellDelegate: NSObjectProtocol {
    func buttonPressed(customCell: UITableViewCell)
}

extension UITableView {
    func layoutTemplateCell(indexPath: IndexPath, viewController: CustomTableViewCellDelegate) -> UITableViewCell {
            let cell = UITableViewCell()

                switch template {
                case customCell:
                    let cell = self.dequeueReusableCell(withIdentifier: customTableViewCell.identifier) as! CustomTableViewCell
                    cell.delegate = viewController
                    return cell
                default:
                    break
                }
            return cell
        }
}

您好,我使用泛型是因为我想为扩展使用更多的一个viewController。假设所有具有tableView的viewController都将使用相同的扩展。仍然不需要使用泛型。布局功能不关心;它需要知道的是,它有一些符合协议的东西。Hi Paul,它之所以有效,是因为您使用的是一个像func layoutTemplateCell这样的viewController(indexPath:indexPath,viewController:CustomTableViewCellDelegate)->UITableViewCell但我想与多个viewController一起使用,因此我必须执行类似于func layoutTemplateCell(indexPath:indexPath,viewController:UIViewController)->UITableViewCell的操作,当我执行此操作时,我会遇到相同的错误否,传递给此函数的任何视图控制器都必须符合协议。如果希望使用UIViewController作为参数类型,则可以有条件地向下转换到
CustomTableViewCellDelegate
,并仅分配向下转换的delegate属性。你在这里所做的一切都不需要泛型。保罗,谢谢你,伙计,我理解你所说的,我修改了我的代码,把演员阵容放到了代表身上,工作了!谢谢!!您好,我使用泛型是因为我想为扩展使用更多的一个viewController。假设所有具有tableView的viewController都将使用相同的扩展。仍然不需要使用泛型。布局功能不关心;它需要知道的是,它有一些符合协议的东西。Hi Paul,它之所以有效,是因为您使用的是一个像func layoutTemplateCell这样的viewController(indexPath:indexPath,viewController:CustomTableViewCellDelegate)->UITableViewCell但我想与多个viewController一起使用,因此我必须执行类似于func layoutTemplateCell(indexPath:indexPath,viewController:UIViewController)->UITableViewCell的操作,当我执行此操作时,我会遇到相同的错误否,传递给此函数的任何视图控制器都必须符合协议。如果希望使用UIViewController作为参数类型,则可以有条件地向下转换到
CustomTableViewCellDelegate
,并仅分配向下转换的delegate属性。你在这里所做的一切都不需要泛型。保罗,谢谢你,伙计,我理解你所说的,我修改了我的代码,把演员阵容放到了代表身上,工作了!谢谢!!