[英]Reload only one section header in UITableView
我在UITableView
使用自定義單元格作為節標題。 在那個單元格中有三個按鈕。 如果在該部分的單元格中單擊任何按鈕,則應僅重新加載該自定義部分單元格,而不是任何行。 這可能嗎?
我使用以下代碼重新加載該單元格:
tableViewHome.reloadSections([1], with: UITableViewRowAnimation.none)
它隱藏了我的部分單元格並扭曲了我的整個表格。
更新
我正在使用UITableView
和我正在使用的以下代碼:
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let cellHeader = tableViewHome.dequeueReusableCell(withIdentifier: "header") as! HeaderTableViewCell
cellHeader.filter1btn.addTarget(self, action: #selector(filterBtnAction(_:)), for: .touchUpInside)
cellHeader.filter2Btn.addTarget(self, action: #selector(filterBtnAction(_:)), for: .touchUpInside)
cellHeader.filter3btn.addTarget(self, action: #selector(filterBtnAction(_:)), for: .touchUpInside)
return cellHeader
}
@IBAction func filterBtnAction(_ sender: UIButton) {
print(sender.tag)
tableViewHome.reloadSections([1], with: UITableViewRowAnimation.none)
}
我有點不清楚這里發生了什么,但聽起來有一個UITableView
概念值得在這里解釋:
UITableView
有自己的單元格概念,實現為UITableViewCell
,還有自己的頁眉/頁腳概念,實現為UITableViewHeaderFooterView
。
根據您的意思是這兩者中的哪一個,您可以采取一些措施來獲得預期的效果:
UITableViewCell
方法: 如果您使用UITableViewCell
作為部分的第一行以充當“標題”,並且您只想重新加載該行以排除該部分的其余部分,則可以調用yourTableViewInstance.reloadRows(at:with:)
(Apple 文檔)此方法采用IndexPath
數組和動畫樣式。 您可以傳入要重新加載的索引路徑。
UITableViewHeaderFooterView
方法: 如果您使用的是正確的UITableViewHeaderFooterView
那么您需要確保在重新加載該部分時提供適當的視圖。 Zack Shapiro概述了您在此答案中需要采取的步驟:
UITableViewHeaderFooterView
的子類。UITableView
實例。viewForHeaderInSection
,你let header = tableView.dequeueReusableHeaderFooterView(withIdentifier: "HeaderView") as! YourHeaderViewSubclass
let header = tableView.dequeueReusableHeaderFooterView(withIdentifier: "HeaderView") as! YourHeaderViewSubclass
他指出的最后一件事是:
具有欺騙性的是函數調用返回
UIView?
當它真的需要dequeuedReusableHeaderFooterView
或reloadData
會導致它消失。
這取決於您采用這兩種實現路徑中的哪一種,但這應該足以為您指明正確的方向。
根據您添加的代碼,它看起來像你打電話yourTableViewInstance.dequeueReusableCell(withIdentifier:for:)
而不是yourTableViewInstance.dequeueReusableHeaderFooterView(withIdentifier:)
內viewForHeaderInSection
。
您需要有一個UITableViewHeaderFooterView
的子類,然后正確調用它。 創建新的子類,然后更改:
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let cellHeader = tableViewHome.dequeueReusableCell(withIdentifier: "header") as! HeaderTableViewCell
// ...
對此:
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let cellHeader = tableViewHome.dequeueReusableHeaderFooterView(withIdentifier: "header") as! HeaderTableView
// ...
您需要在此處執行兩個步驟:
UITableViewHeaderFooterView
而不是UITableViewCell
。是的。
假設這是您的方法的實現:
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let customCell = .... as! YourCustomCell
customCell.someLabel.text = "Some Data"
//... filling your curstom cell
return customCell
}
你可以用這種方式改變它
func updateHeaderView(headerView:YourCustomCell, section: Int) {
customCell.someLabel.text = "Some Data"
//... filling your curstom cell
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let customCell = .... as! YourCustomCell
self.updateHeaderView(customCell, section)
return customCell
}
並在需要時再次調用self.updateHeaderView(customCell, section)
,例如
func buttonClicked() {
let customCell = self.tableView.headerView(forSection: 0) as! YourCustomCell
self.updateHeaderView(customCell, 0)
}
我認為您的標題視圖類正在擴展 UITableViewHeaderFooterView 類。 在擴展中添加功能
extension UITableViewHeaderFooterView{
func reloadHeaderCell(){
preconditionFailure("This method must be overridden")
}
}
現在在您的 Header 類中覆蓋它,如下所示
class HeaderView: UITableViewHeaderFooterView {
override func reloadHeaderCell() {
////// add your logic to reload view
}
}
現在你可以簡單地調用下面的行來刷新視圖
self.tableView?.headerView(forSection:0)?.reloadHeaderCell()
我所做的並且工作得非常正確,請按照給定的答案進行操作:
斯威夫特 3.1
第 1 步:
首先,我view
xib,根據我的要求設計了它,並在我要求的課程中注冊了。
其次,做了子類class HeaderView: UITableViewHeaderFooterView
的UITableViewHeaderFooterView
像下圖:
在我需要的課程(這里是家庭課程)中,我確實為我的 tableview 注冊了我的xib
文件。
override func viewDidLoad() {
super.viewDidLoad()
tableViewHome.register(UINib(nibName: "HeaderView", bundle: nil), forHeaderFooterViewReuseIdentifier: "HeaderView")
}
第 2 步:
然后在我要求的課程中,我做了以下事情:
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let cellHeader = tableViewHome.dequeueReusableHeaderFooterView(withIdentifier: "HeaderView") as! HeaderView
cellHeader.filterAction(cellHeader.filter1Btn)
return cellHeader
}
它開始按照我的要求工作,后來我在我的班級中添加了自定義委托以進行更多操作,但是通過子查看,它現在可以工作了。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.