【问题标题】:UITableViewCell inside PageViewController behaving differentlyPageViewController 中的 UITableViewCell 表现不同
【发布时间】:2016-03-05 16:50:56
【问题描述】:

我将工作 tableviewcontroller 放在 pageviewcontroller 中。现在我必须以编程方式在表格视图中注册单元格,并且不会为单元格调用方法 awakeFromNib。如果我尝试设置内容,我的自定义单元格中的所有属性都未初始化,并且应用程序崩溃。

当我在 pageviewcontroller 中添加 tableviewcontroller 时有什么不同吗?

self.tableView.registerClass(ParticipantCell.self, forCellReuseIdentifier: ParticipantCell.cellIdentifier)

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let index = self.sections[indexPath.section].index+indexPath.row

    let participant = self.participants[index]

    let cell = tableView.dequeueReusableCellWithIdentifier(ParticipantCell.cellIdentifier, forIndexPath: indexPath) as! ParticipantCell

   // cell.setParticipantData(participant)


    return cell
}

【问题讨论】:

    标签: ios uitableview uipageviewcontroller


    【解决方案1】:

    如果您以编程方式创建单元格,则不会调用 awakeFromNib。但是,您可以通过覆盖来进行配置:

    init(style: UITableViewCellStyle, reuseIdentifier: String?)
    

    作为建议,您可以使用这样的方法:

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupCell()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setupCell()
    }
    
    func setupCell() {
        // setup your cell here
    }
    

    【讨论】:

    • 所以我必须以编程方式添加每个控件?一切都在 IB 中定义
    • 通过从情节提要加载视图控制器来更改此行为。现在我不需要注册单元并从笔尖唤醒被调用。但属性仍然为零
    • 我会仔细检查情节提要中的所有元素是否与您的类属性相关联,并且我会仔细检查您是否仅在创建/绘制单元格后引用属性(我已经之前在元素生命周期的错误时间引用元素的错误)。例如,如果你在 viewController 的 viewDidLoad 方法中引用了一个 UILabel,那么它就是 nil。在创建 nib/storyboard 后,必须在 viewWillAppear/viewDidAppear 中操作标签。
    【解决方案2】:

    问题是 Tableviewcontroller 实例化错误。我通过从情节提要中实例化它来解决它:

    let storyboard = UIStoryboard(name: "Participants", bundle: NSBundle.mainBundle())
    
    if let controller = storyboard.instantiateViewControllerWithIdentifier("ParticipantListController") as? ParticipantListController{
    ...
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-30
      • 2011-06-02
      • 1970-01-01
      • 1970-01-01
      • 2017-05-08
      相关资源
      最近更新 更多