Swift(RxSwift):使用泛型链接ViewItem和Cell类

时间:2018-11-27 11:41:51

标签: ios swift generics rx-swift

我正在重构 RxSwift 中的UITableView实现。我有不同的视图项,我想对其进行切换,以便能够相应地配置我的dataSource。我需要知道哪个单元格类属于哪个视图项。

为此,我编写了一个方法,该方法采用一个实现我的ViewItemProtocol并返回AnyClass?的项目。

func getAssociatedtCellType<T>(for item: T) -> AnyClass? where T: ViewItemProtocol {
    switch item  {
    case is TextFieldViewItem: return TextFieldCell.self
    case is FaqDetailViewItem: return FaqCell.self
    case is HeaderViewItem: return HeaderCell.self
    case is SubmitButtonViewItem: return SubmitButtonCell.self
    case is BankDetailViewItem: return BankDetailCell.self
    case is EmailViewItem: return EmailCell.self
    default: return nil
    }
}

函数方法配置了我的单元格

private func configCell<T>(for item: T, with identifier: String, at indexPath: IndexPath) -> UITableViewCell where T: ViewItemProtocol {
    guard let cellType = getAssociatedtCellType(for: item.self) else { return UITableViewCell() }
    guard let cell = tableView.dequeueReusableCell(withIdentifier: identifier, for: indexPath) as? cellType else { fatalError() }
    cell.configureBindings(itemSource: item)
    return cell
}

问题cellType出现:

  

使用未声明的类型“ cellType”

背后的目的是重构这种方法:

private func dataSource() -> RxTableViewSectionedReloadDataSource<SectionedViewItem> {

    let dataSource = RxTableViewSectionedReloadDataSource<SectionedViewItem>(configureCell: { [unowned self] _, tableView, indexPath, item in

        switch item {
        case let item as TextFieldViewItem:
            guard let cell = tableView.dequeueReusableCell(withIdentifier: TextFieldCell.Key, for: indexPath) as? TextFieldCell else { fatalError() }
            cell.configureBindings(itemSource: item)
            cell.changeButton.isHidden = item.editable
            cell.changeButton.addTarget(self, action: #selector(self.showFieldNotEditableAlert), for: .touchUpInside)
            cell.textField.addTarget(self, action: #selector(self.textFieldDidChange), for: .editingChanged)
            return cell

        case let item as FaqDetailViewItem:
            guard let cell = tableView.dequeueReusableCell(withIdentifier: FaqDetailCell.Key, for: indexPath) as? FaqDetailCell else { fatalError() }
            cell.configureBindings(itemSource: item)
            return cell

        case let item as PasswordTextFieldViewItem:
            guard let cell = tableView.dequeueReusableCell(withIdentifier: PasswordTextFieldCell.Key, for: indexPath) as? PasswordTextFieldCell else { fatalError() }
            cell.configureBindings(itemSource: item)
            return cell

        case let item as HeaderViewItem:
            guard let cell = tableView.dequeueReusableCell(withIdentifier: HeaderCell.Key, for: indexPath) as? HeaderCell else { fatalError() }
            cell.configureBindings(itemSource: item)
            return cell

// ... My other cell types... :-(

    })

    dataSource.titleForHeaderInSection = { dataSource, index in
        let section = dataSource[index]
        return section.header
    }

    return dataSource
}

...变成这样:

private func dataSource() -> RxTableViewSectionedReloadDataSource<SectionedViewItem> {

    let dataSource = RxTableViewSectionedReloadDataSource<SectionedViewItem>(configureCell: { [unowned self] _, tableView, indexPath, item in

        switch item {
        case let item as TextFieldViewItem:
            guard let cell = tableView.dequeueReusableCell(withIdentifier: TextFieldCell.Key, for: indexPath) as? TextFieldCell else { fatalError() }
            cell.configureBindings(itemSource: item)
            cell.changeButton.isHidden = item.editable
            cell.changeButton.addTarget(self, action: #selector(self.showFieldNotEditableAlert), for: .touchUpInside)
            cell.textField.addTarget(self, action: #selector(self.textFieldDidChange), for: .editingChanged)
            return cell

        case let item as FaqDetailViewItem, let item as PasswordTextFieldViewItem, let item as HeaderViewItem:
            return configCell(for: item, with: "Cell.Key... still to implement)", at: indexPath)


        default:
            return UITableViewCell()
        }

    })

    dataSource.titleForHeaderInSection = { dataSource, index in
        let section = dataSource[index]
        return section.header
    }

    return dataSource
}

1 个答案:

答案 0 :(得分:1)

不幸的是,这无法在Swift中完成。快速地,编译器需要在编译时知道变量的类型,但是在您的情况下,您尝试在运行时进行下转换。所以不,您将无法做到这一点。

您可能可以在运行时通过泛型进行某种转换来完成它,但是强制转换不会让您访问该类的属性,这可能是您进行所有绑定所需要的。我认为最好将switch语句中的代码移到tableViewCell设置中。