0

I am trying to determine which section of the UITableView is visible (on top, if there are 2 sections simultaneously), in swift 2. So far I found this solution: Is there any way to know if the user is scrolling tableview up or down? in swift IOS , this: How to get a UITableView's visible sections? and this one: Swift - UITableView scroll event

All of them not working for me for reasons:

1. I can not add UIScrollViewDelegate, because swift 2 is giving error on redundant conforming to protocol (because it is UITalbeView, and it already conforms to UIScrollView).

2. However, this:

func scrollViewDidScroll () {
    print("scrolled: \(self.tableView.indexPathsForVisibleRows)")
}

is never being launched.

Delegate and data source for the UITableView are defined through storyboard interface builder.

How would you determine in swift 2 that UITableViewController is being scrolled, and which section is visible?

Community
  • 1
  • 1
Async-
  • 3,140
  • 4
  • 27
  • 49

1 Answers1

4

please try to get visible cell from willDisplayCell method and then get section from this visible cell. For Example: First get Indexpath from Cell and then retrive section from Indexpath.

let section = indexPath.section

Code is:

override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
    let cell = self.tableView.dequeueReusableCellWithIdentifier("CustomCell") as! CustomCellViewController

    let position: CGPoint = cell.convertPoint(CGPointZero, toView: self.tableView)
    if let indexPath = self.tableView.indexPathForRowAtPoint(position)
    {
        let section = indexPath.section
        print("will display section: \(section)")
    }
}
Async-
  • 3,140
  • 4
  • 27
  • 49
Vvk
  • 4,031
  • 29
  • 51