How do I scroll a UITableView to a section that contains no rows?

后端 未结 6 698
盖世英雄少女心
盖世英雄少女心 2020-12-04 12:53

In an app I\'m working on, I have a plain style UITableView that can contain a section containing zero rows. I want to be able to scroll to this section using scrollToRowAtI

6条回答
  •  有刺的猬
    2020-12-04 13:18

    A Swift approach to the same:

    if rows > 0 {
        let indexPath = IndexPath(row: 0, section: section)
        self.tableView.setContentOffset(CGPoint.zero, animated: true)
        self.tableView.scrollToRow(at: indexPath, at: .top, animated: true)
    }
    
    else {
        let sectionRect : CGRect = tableView.rect(forSection: section)
        tableView.scrollRectToVisible(sectionRect, animated: true)
    }
    

提交回复
热议问题