2017-09-15 62 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