2017-09-15 116 views
1

我正在開發一個公司的項目,在iOS 11 GM上測試我的表格視圖時遇到此問題。它在iOS 10上工作得很好。很簡單,我有三個帶標題的部分。當我點擊部分標題時,我的部分會崩潰/延伸。下面是我如何做到這一點:iOS 11重新裝載部分創建重複標題部分

的ViewController:

class ViewController: UIViewController { 

@IBOutlet weak var tableView: UITableView! 

var sectionsOpened: [String: Bool] = [ 
    "section1": true, 
    "section2": true, 
    "section3": true 
] 

func isSectionOpened(section: String) -> Bool { 
    return sectionsOpened[section]! 
} 

@objc func toggleSection1() { 
    sectionsOpened["section1"] = !sectionsOpened["section1"]! 
    toggle(sectionIndex: 0) 
} 

@objc func toggleSection2() { 
    sectionsOpened["section2"] = !sectionsOpened["section2"]! 
    toggle(sectionIndex: 1) 
} 

@objc func toggleSection3() { 
    sectionsOpened["section3"] = !sectionsOpened["section3"]! 
    toggle(sectionIndex: 2) 
} 

func toggle(sectionIndex: Int) { 

    self.tableView.reloadSections([sectionIndex], with: .automatic) 
    self.tableView.scrollToRow(at: IndexPath(row: 0, section: sectionIndex), at: .top, animated: true) 
} 

表視圖數據源:

extension ViewController: UITableViewDataSource { 
func numberOfSections(in tableView: UITableView) -> Int { 
    return 3 
} 

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return 1 
} 

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "testCell", for: indexPath) 
    let label = cell.viewWithTag(1) as! UILabel 
    label.text = "TEST \(indexPath.section) - \(indexPath.row)" 
    return cell 
} 

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { 
    let headerView = UILabel(frame: CGRect(x: 0, y: 0, width: 320, height: 60)) 
    headerView.backgroundColor = UIColor.green 
    headerView.text = "Header \(section)" 
    var gesture: UITapGestureRecognizer? 
    if section == 0 { 
     gesture = UITapGestureRecognizer(target: self, action: #selector(toggleSection1)) 
    } else if section == 1 { 
     gesture = UITapGestureRecognizer(target: self, action: #selector(toggleSection2)) 
    } else if section == 2 { 
     gesture = UITapGestureRecognizer(target: self, action: #selector(toggleSection3)) 
    } 
    headerView.addGestureRecognizer(gesture!) 
    headerView.isUserInteractionEnabled = true 
    return headerView 
} 

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { 
    return 60 
} 

func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat { 
    return CGFloat.leastNormalMagnitude 
} 

表視圖委託:

extension ViewController: UITableViewDelegate { 
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 
    if !isSectionOpened(section: "section\(indexPath.section+1)") { 
     return 1 
    } 
    if indexPath.section == 0 { 
     return 350 
    } else if indexPath.section == 1 { 
     return 400 
    } else if indexPath.section == 2 { 
     return 350 
    } 
    return 500 
} 

首先注意到的是,scrollToRow行爲怪異,其轉到頂部,然後向下滾動到該位置。 然後,試圖打開/關閉後的3頭,滾動向上/向下,有時我得到了這個重複的標題問題:

enter image description here
Duplicate header when reloading sections (photo) '

預先感謝您的幫助。我真的需要使這個工作,因爲iOS的11將在下週二到來......

+0

迄今發現了一個解決方案? tableView.estimatedRowHeight = 0的作品。但是,我不想手動計算所有行高。 –

回答

3

我有同樣的問題,並通過使用固定它:

self.tableView.estimatedRowHeight = 0; 
self.tableView.estimatedSectionHeaderHeight = 0; 
self.tableView.estimatedSectionFooterHeight = 0; 
+0

如果我們將estimatedRowHeight設置爲零,那麼自定義單元格高度與約束呢? – Sabby

+0

我不知道,因爲我沒有使用限制 – Supala

+0

很酷..看起來它不能用於自我大小的細胞..所以我們必須手動計算高度.. – Sabby

1

問題與預計行高度來完成當使用自我大小的細胞時。看起來,如果我們沒有提供非常準確的估計值,當重新加載表視圖行時,在不同版本的iOS中可能會出現一些問題。在iOS 9和10上,表格視圖可能跳轉到頂部。在iOS 11上,如果在重新加載行時它們在頂部粘滯,則部分標題可能會重複。

一種解決方案是緩存顯示行的高度,然後在需要時將此高度提供回tableview作爲估計高度。惱人不得不訴諸這樣的事情,但它在iOS 9,10日爲我解決了這兩個問題,11

參見this issue