【问题标题】:UITableViewCell as UIView from XIBUITableViewCell 作为来自 XIB 的 UIView
【发布时间】:2017-09-24 19:57:55
【问题描述】:

我在XIB中有CustomTableViewClass,把它加到UITableView就好了

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 而崩溃

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

【问题讨论】:

  • 您是否在 storyBoard 中分配了 CustomTableViewClass 类?

标签: ios uitableview uiview xib nscoder


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    试试这个代码:

        var views:NSArray = NSBundle.mainBundle().loadNibNamed("Your_Xib_file", owner: self, options: nil);
        var yourView:UIView = views.objectAtIndex(0) as! UIView;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-12
      • 1970-01-01
      相关资源
      最近更新 更多