我想定义一个函数,给一些参数返回一个UITableViewCell
的具体实现,如下所示:
func cellClassFromCellType<T: UITableViewCell>(cellType: CellType, atIndexPath: NSIndexPath) -> T.Type {
switch cellType
case someEnum: return NotificationCell
default: return UITableViewCell
}
所以我可以这样做:
let cellType = cellClassFromCellType(.Test, atIndexPath: NSIndexPath())
let cell = cellType(style: UITableViewCellStyle.Default, reuseIdentifier: "")
但是通过上面的实现,我得到以下错误:
Cannot convert return expression of type 'NotificationCell.Type' to return type 'T.Type'
我甚至不确定上述实现方向是否正确,因此任何输入都会有所帮助。
最好我也想在protocol
中定义这个函数,所以也非常感谢定义这样一个协议的一些帮助。