如何在不使用nib的情况下注册要重用于UITable视图头的UIView

时间:2016-05-10 09:06:58

标签: ios swift uitableview

我在故事板上创建了一个UIView,我希望将此视图用于我的UITableView的标题。我在这个视图的storyboard上给了一个标识符,在viewForHeaderInSection类中我将这个标识符出列,如下所示。但是,每当我重新加载表时,我发现headerCell是一个新实例。在这种情况下不会重复使用它。

override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let  headerCell = tableView.dequeueReusableCellWithIdentifier("HeaderCell") as! UserHomeHeaderCell

我试图在viewDidLoad()方法上注册这个类,如下所示:

tableView.registerClass(UserHomeHeaderCell.classForCoder, forCellReuseIdentifier: "HeaderCell")

但是与故事板链接的所有单元格内容都是零。我该如何重用这个标题?

2 个答案:

答案 0 :(得分:0)

试试这个:

override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let  headerCell = tableView.dequeueReusableCellWithIdentifier("HeaderCell") as! UserHomeHeaderCell
    return headerCell.contentView
}

答案 1 :(得分:-1)

您必须在ViewDidLoad()

中注册nib
tableView.registerNib(UINib(nibName: "UserHomeHeaderCell" , bundle: nil), forHeaderFooterViewReuseIdentifier: HeaderCell)