在 tableView 中添加 UITableView 作为 viewForFooterInSection
Add UITableView as viewForFooterInSection within a tableView
我正在尝试在前一个 tableView 的末尾添加一个自定义 UIView 循环,因此我在同一个 VC.
中有两个连续的列表
因为我希望滚动是全局的而不是分成两个单独的 tableView,所以我尝试在 viewForFooterInSection 方法中使用循环添加内容。
正如您想象的那样,它没有按计划进行。调试器没有给我任何错误,但我的 UI 也没有显示任何内容。我想知道是否有人知道它是如何实现的。
这是我想做的事情的屏幕:
如您所见,它应该是两个连续的 tableView,但我不希望它们滚动时彼此分开。我希望页面的流动是一个大卷轴。
这是我的代码:
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
let footerView = UIView()
self.getBestUsers( completion: { // API call
self.bestUsers.enumerated().forEach { (index, user) in // Loop
DispatchQueue.main.async {
let footer = self.debateList.dequeueReusableCell(withIdentifier: "bestUsersBox") as! UserBox
self.tableView.tableFooterView = footer
footer.setNeedsLayout()
footer.layoutIfNeeded()
footer.frame.size = footer.systemLayoutSizeFitting(UIView.layoutFittingCompressedSize)
footer.userBoxView.user = user
footerView.addSubview(footer)
footer.topAnchor.constraint(equalTo: footerView.topAnchor, constant: 0).isActive = true
footer.bottomAnchor.constraint(equalTo: footerView.bottomAnchor, constant: 0).isActive = true
footer.leftAnchor.constraint(equalTo: footerView.leftAnchor, constant: 0).isActive = true
footer.rightAnchor.constraint(equalTo: footerView.rightAnchor, constant: 0).isActive = true
}
}
})
footerView.translatesAutoresizingMaskIntoConstraints = false
return footerView
}
[1]: https://i.stack.imgur.com/9pN9u.png
所以,我想出了如何实现我想要的。抱歉,如果不清楚。
我只是使用了两个不同的部分来添加不同来源的内容:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let loadCell = debateList.dequeueReusableCell(withIdentifier: "loadMore", for: indexPath) as! LoadMore
if indexPath.section == 0 {
let debateCell = debateList.dequeueReusableCell(withIdentifier: "debateBox", for: indexPath) as! DebateBox
if trendingDebates.count > 0 {
if (indexPath.row == trendingDebates.count) {
loadCell.buttonTapCallback = {
self.loadMoreDebates()
}
if trendingDebates.count < totalItems {
return loadCell
} else {
return UITableViewCell()
}
} else {
let currentDebate = trendingDebates[indexPath.row]
debateCell.debateBoxView.debate = currentDebate
return debateCell
}
}
} else {
let userCell = debateList.dequeueReusableCell(withIdentifier: "bestUsersBox", for: indexPath) as! UserBox
userCell.userBoxView.user = bestUsers[indexPath.row]
return userCell
}
return UITableViewCell()
}
我正在尝试在前一个 tableView 的末尾添加一个自定义 UIView 循环,因此我在同一个 VC.
中有两个连续的列表因为我希望滚动是全局的而不是分成两个单独的 tableView,所以我尝试在 viewForFooterInSection 方法中使用循环添加内容。
正如您想象的那样,它没有按计划进行。调试器没有给我任何错误,但我的 UI 也没有显示任何内容。我想知道是否有人知道它是如何实现的。
这是我想做的事情的屏幕:
如您所见,它应该是两个连续的 tableView,但我不希望它们滚动时彼此分开。我希望页面的流动是一个大卷轴。
这是我的代码:
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
let footerView = UIView()
self.getBestUsers( completion: { // API call
self.bestUsers.enumerated().forEach { (index, user) in // Loop
DispatchQueue.main.async {
let footer = self.debateList.dequeueReusableCell(withIdentifier: "bestUsersBox") as! UserBox
self.tableView.tableFooterView = footer
footer.setNeedsLayout()
footer.layoutIfNeeded()
footer.frame.size = footer.systemLayoutSizeFitting(UIView.layoutFittingCompressedSize)
footer.userBoxView.user = user
footerView.addSubview(footer)
footer.topAnchor.constraint(equalTo: footerView.topAnchor, constant: 0).isActive = true
footer.bottomAnchor.constraint(equalTo: footerView.bottomAnchor, constant: 0).isActive = true
footer.leftAnchor.constraint(equalTo: footerView.leftAnchor, constant: 0).isActive = true
footer.rightAnchor.constraint(equalTo: footerView.rightAnchor, constant: 0).isActive = true
}
}
})
footerView.translatesAutoresizingMaskIntoConstraints = false
return footerView
}
[1]: https://i.stack.imgur.com/9pN9u.png
所以,我想出了如何实现我想要的。抱歉,如果不清楚。
我只是使用了两个不同的部分来添加不同来源的内容:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let loadCell = debateList.dequeueReusableCell(withIdentifier: "loadMore", for: indexPath) as! LoadMore
if indexPath.section == 0 {
let debateCell = debateList.dequeueReusableCell(withIdentifier: "debateBox", for: indexPath) as! DebateBox
if trendingDebates.count > 0 {
if (indexPath.row == trendingDebates.count) {
loadCell.buttonTapCallback = {
self.loadMoreDebates()
}
if trendingDebates.count < totalItems {
return loadCell
} else {
return UITableViewCell()
}
} else {
let currentDebate = trendingDebates[indexPath.row]
debateCell.debateBoxView.debate = currentDebate
return debateCell
}
}
} else {
let userCell = debateList.dequeueReusableCell(withIdentifier: "bestUsersBox", for: indexPath) as! UserBox
userCell.userBoxView.user = bestUsers[indexPath.row]
return userCell
}
return UITableViewCell()
}