awakeFromNib() 调用了两次

awakeFromNib() called twice

我有一个表格视图,其中注册了两个自定义 nib 单元格。细胞和它们对应的连接 类 是完全独立的。

基于条件的表视图应显示注册的两个单元格之一:

表格视图代码:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    //Show only the table cells that have content.
    tableView.tableFooterView = UIView(frame: CGRect.zero)

    let rowData = FriendsTab.potentialFriendList[indexPath.row]

    if (rowData.pendingInvite){
        let cell = potentialFriendTableView.dequeueReusableCell(
            withIdentifier: requestCellIdentifier, for: indexPath)
            as! FriendRequestCell

        cell.activityIndicator.isHidden = true
        cell.activityIndicator.hidesWhenStopped = true

        cell.userName.text = firstName + " " + lastName

        return cell
    }
    else{
        let cell = potentialFriendTableView.dequeueReusableCell(
            withIdentifier: potentialCellIdentifier, for: indexPath)
            as! PotentialFriendCell


        cell.activityIndicator.isHidden = true
        cell.activityIndicator.hidesWhenStopped = true

        cell.userName.text = firstName + " " + lastName


        return cell
    }
}

小区注册码(来自viewDidLoad):

    let potentialFriendXib = UINib(nibName: self.potentialCellIdentifier, bundle: nil)
    let requestFriendXib = UINib(nibName: self.requestCellIdentifier, bundle: nil)

    self.potentialFriendTableView.register(potentialFriendXib,forCellReuseIdentifier: self.potentialCellIdentifier)
    self.potentialFriendTableView.register(requestFriendXib,forCellReuseIdentifier: self.requestCellIdentifier)

出于某种原因,FriendRequestCell 调用了 awakeFromNib() 两次,但 PotentialFriendCell 仍按预期工作(一次调用)。

我已经搜索了几个(非常老的)SO 问题,但它们似乎处理这个项目不使用的嵌套(父子)笔尖(它们是单独的)。

有没有我哪里出错的想法?​​

所有这些都是在 InterfaceBuilder 中完成的。在界面生成器中,打开包含 tableviewcontroller 的故事板或 xib。然后 select 使用 Content: Dynamic Prototypes,select 2 用于 Prototype Cells。然后您将获得 2 个 TableView 单元格。下一步是完全按照您对每个 xib 中的单元格所做的操作进行操作。现在没有加载笔尖,也没有注册。您仍然需要为每个单元格执行 dequeueReusableCell。