【发布时间】:2019-07-19 20:03:24
【问题描述】:
我有一个表格视图,其中注册了两个自定义笔尖单元。细胞和它们相应的连接类是完全相互独立的。
基于条件的表格视图应该显示两个注册单元格之一:
tableview代码:
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 问题,但它们似乎处理了这个项目不使用的嵌套(父子)笔尖(它们是单独的)。
任何想法我哪里出错了?
【问题讨论】:
-
我不确定为什么 FriendRequestCell 调用 awakeFromNib() 两次。另一种选择是在 tableView 中创建 2 个自定义单元格,由 IB 中的 potentialFriendTableView 表示。
-
谢谢,你能给我详细一点吗?我拥有的自定义单元格已经注册到 tableView 潜在朋友表视图?
-
所有这些都在 InterfaceBuilder 中完成。在界面生成器中,打开包含 tableviewcontroller 的故事板或 xib。然后选择使用Content:Dynamic Prototypes,选择2 for Prototype Cells。然后,您将获得 2 个 TableView 单元格。下一步是完全按照您对每个 xib 中的单元格所做的操作。现在没有加载笔尖,也没有注册。您仍然需要为每个单元格执行 dequeueReusableCell。
-
感谢 Loren,这似乎已经解决了这个问题。只需将两个原型单元格添加到 tableview,上面的代码或多或少保持不变并且它可以工作?!?如果您将此添加为答案,我会接受。谢谢。
标签: ios swift xcode uitableview xib