ios - 为不同的 UITableViewCell 类创建单个 .xib

标签 ios swift uitableview xib

我遇到了一个问题

我有一个 NNVerticalStackTableViewCell.xib 文件及其对应的 NNVerticalStackTableViewCell.swift 文件。

NNVerticalStackTableViewCell.swift 文件的代码

class NNVerticalStackTableViewCell: UITableViewCell
{
    //MARK: Outlets
    @IBOutlet weak var titleLabel: UILabel!
    @IBOutlet weak var verticalStack: NNVerticalStackView!

    //MARK: Internal Properties
    var titleString : String?
        {
        set{
            self.titleLabel.text = newValue
        }
        get{
            return self.titleLabel.text
        }
    }

    //MARK: View Lifecycle Methods
    override func awakeFromNib()
    {
        super.awakeFromNib()
        self.titleLabel.text = nil
    }

    //MARK: Internal Methods
    func addViewsInVerticalStack(viewsArray : [UIView])
    {
        for view in viewsArray
        {
            self.verticalStack.insertStackItem(view)
        }
    }
}

NNVerticalStackTableViewCell.xib 文件的接口(interface)

enter image description here

类名为:NNVerticalStackTableViewCell单元格标识符为:NNVerticalStackTableViewCell

现在我创建了一个 NNVerticalStackTableViewCell 的子类:

class NNPropertiesUnderProjectTableViewCell: NNVerticalStackTableViewCell
{
    //MARK: Internal Properties
    var completionHandler : ((NNSearchPreference) -> Void)?

    //MARK: Internal Methods
    func configureCell(_ propertiesUnderProjectArray : [[String : Any]])
    {
        var viewsArray = [UIView]()
        for dict in propertiesUnderProjectArray
        {
            let elementView = NNPropertiesUnderProjectElementView.loadFromNib()
            elementView?.configureViewWith(buttonTitleString: dict["displayString"] as! String, searchPreference: dict["searchPreference"] as! NNSearchPreference, completionHandler: {[weak self] (searchPreference) in
                if let handler = self?.completionHandler
                {
                    handler(searchPreference)
                }
                })
            viewsArray.addObject(elementView)
        }
        self.addViewsInVerticalStack(viewsArray: viewsArray)
    }
}

现在我想做的是将 NNPropertiesUnderProjectTableViewCell 类与 NNVerticalStackTableViewCell.xib 接口(interface)一起使用。

类似于 NNPropertiesUnderProjectTableViewCell 类,我有许多其他类使用与 NNVerticalStackTableViewCell.xib 相同的接口(interface)并相应地自定义它。

在我的 ViewController 中,我注册了 NNVerticalStackTableViewCell.xib,然后将单元格创建为:

self.tableView.register(UINib(nibName: "NNVerticalStackTableViewCell"), forCellReuseIdentifier: "NNVerticalStackTableViewCell")


func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {
            let propertiesUnderProjectCell = tableView.dequeueReusableCell(withIdentifier: "NNVerticalStackTableViewCell", for: indexPath as IndexPath) as! NNPropertiesUnderProjectTableViewCell
    }

但是,我遇到了异常:

Could not cast value of type 'NNVerticalStackTableViewCell' (0x100945878) to 'NNPropertiesUnderProjectTableViewCell' (0x100949330).

任何人都可以建议如何解决这个问题。我想使用单个 .xib 并且不想重复代码。

最佳答案

你可以,但是,不要使用 UITableViewCell,而是使用类似的 UIView

为单元格内容 View 创建一个 xib,给它一个类(如 CellView)。 在每个单元格类中,通过loadNibNamed获取CellView,并将其设置为单元格内容 View ,然后可以自由更改CellView的属性并将其用作其他单元类的内容 View

也许有更好的解决方案,但对于外观相似的 UIViewController,我就是这样做的,您也可以尝试在 Storyboard 中未设置类的 cell xib 并尝试加载它,看看错误是否仍然存在还是不是

关于ios - 为不同的 UITableViewCell 类创建单个 .xib,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40501574/

相关文章:

ios - 从 UITapGestureRecognizer 获取 UITableViewCell

ios - 查询 TableView Controller

ios - 防止 UITableView 滚动到某个点以下

swift - 在 Swift 3 和 Xcode 8 中从嵌套字典中获取值的问题

ios - 整理 MPMediaQuery 时转义标点符号和歌曲标题中的 'The' 前缀

ios - 如何在 Swift 中的滑出菜单的主 UIView 后面添加 UIView?

ios - 自定义 CSS 字体在 PC 上加载,而不是在 IOS 上加载

ios - Swift 2 官方指南/文档链接?

ios - 如何使用动态大小更改 Swift 中 iOS 应用程序的字体?

swift - 使用自动布局约束旋转后对齐 UILabel 文本