UITableViewCell 作为来自 XIB 的 UIView [英] UITableViewCell as UIView from XIB

查看:32
本文介绍了UITableViewCell 作为来自 XIB 的 UIView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 XIB 中有 CustomTableViewClass,将它添加到 UITableView 就像

I have CustomTableViewClass in XIB, adding it to UITableView like

class RestaurantsTableView: UITableView {
    override func awakeFromNib() {
        self.register(UINib(nibName: "RestaurantTableViewCell", bundle: nil), forCellReuseIdentifier: "restaurantCell")
    }
}

一切正常,现在我想在其他一些UIViewController中使用这个CustomTableViewClass作为UIView,但我不明白如何正确覆盖它的 init(coder aCoder: NSCoder) 函数,因为我不需要 UITableView 的情况,当我像其他自定义 XIB 视图一样实现它时nil

and everything works fine, now I would like to use this CustomTableViewClass as UIView in some other UIViewController, but I do not get how to properly override its init(coder aCoder: NSCoder) function, cause I do not need one for UITableView case and when I implement it like I do for other custom XIB views it crashes with nil

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    let _ = Bundle.main.loadNibNamed("RestaurantTableViewCell", owner: self, options: nil)?[0] as! UIView
}

推荐答案

有一种方法可以使用 contentView UITableViewCell 的属性,属于 UIView 类型.因此,只需像之前从 nib 获取的那样获取 RestaurantTableViewCell,然后使用它的 contentView 从中获取 UIView.

There is one way you can use contentView property of UITableViewCell which of type UIView. So simply get RestaurantTableViewCell as you previously getting from nib then use its contentView to get the UIView from it.

let cell = Bundle.main.loadNibNamed("RestaurantTableViewCell", owner: self, options: nil)?[0] as! RestaurantTableViewCell
let yourView = cell.contentView

这篇关于UITableViewCell 作为来自 XIB 的 UIView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆