1

I have this table view whose table view cells have the width and the height of the screen. Each showing an image. While you scroll through the table view you can have one table view cell occupying the whole screen. Or at the most you can see 2 table view cells on the screen of the app at the same time. My question is: is there any way I can find out which of the 2 table view cells occupies more screen height than the other?

Avi
  • 7,150
  • 1
  • 18
  • 22
asheyla
  • 2,597
  • 4
  • 14
  • 30

3 Answers3

4

Here is the easy way to find out which cell is visible more than 60%.

func scrollViewDidScroll(scrollView: UIScrollView) {

    checkWhichVideoToEnable()
}

func checkWhichVideoToEnable() {

    for cell in tableView.visibleCells as [UITableViewCell] {

        if cell.isKindOfClass(UITableViewCell) {

            let indexPath = tableView.indexPathForCell(cell)
            let cellRect = tableView.rectForRowAtIndexPath(indexPath!)
            let superView = tableView.superview

            let convertedRect = tableView.convertRect(cellRect, toView: superView)
            let intersect = CGRectIntersection(tableView.frame, convertedRect)
            let visibleHeight = CGRectGetHeight(intersect)

            if visibleHeight > self.view.bounds.size.height * 0.6 {  // only if 60% of the cell is visible.

                //cell is visible more than 60%
                print(indexPath?.row) //your visible cell.
            }
        }
    }
}

Reference from HERE.

Community
  • 1
  • 1
Dharmesh Kheni
  • 67,254
  • 32
  • 154
  • 160
3

Look at the second visible cell (tableView.visibleCells[1]) and check its y position. If it's less than half the height of the tableView, it's taking up more of the screen, else less. Break ties any way you want.

edit

The y position for a given indexPath is fixed, so you need to take into account the tableView's contentOffset.

CGFloat yOrigin = [tableView.visibleCells[1] frame].origin.y - tableView.contentOffset.y;
Avi
  • 7,150
  • 1
  • 18
  • 22
  • I tried like this: print(tableView.visibleCells[0].frame.origin.y) print(tableView.visibleCells[1].frame.origin.y) And no matter how i move the 2 cells on the screen(higher or lower), for the first 2 i get: 568 and 1136, while for the second and third I get 1136 and 1704. It doesn't seem to matter which takes more screen or less, the frame.origin.y doesn't change. – asheyla Jan 20 '16 at 12:44
  • Right. You have to take into account the tableView's contentOffset. I'll update my answer. – Avi Jan 20 '16 at 12:50
  • This is because view.fram always refers to the inner coordinate system of the very superview. You could convert the coordinates to those of self.view. UIView has build-in methods for point conversions. You could convert it to main window or device coordinates too, but that can be very confusing when rotations apply plus the origin (0.0) of the device is different from the UIKit coodrinate system. – Hermann Klecker Jan 20 '16 at 12:55
  • Worked perfectly, Avi! Thank you! wrote it like this for Swift: let yOrigin1: CGFloat = tableView.visibleCells[0].frame.origin.y - tableView.contentOffset.y let yOrigin2: CGFloat = tableView.visibleCells[1].frame.origin.y - tableView.contentOffset.y print("-------- \(yOrigin1)") print("-------- \(yOrigin2)") – asheyla Jan 20 '16 at 13:15
0

you could maybe try to get the tableView's visibleCells and check the origin of those cells. Since you'll have 2 visibleCells at any time, comparing their respective frame's y value could tell you which is occupying more height

Ramy Kfoury
  • 929
  • 5
  • 8
  • I tried like this: print(tableView.visibleCells[0].frame.origin.y) print(tableView.visibleCells[1].frame.origin.y) And no matter how i move the 2 cells on the screen(higher or lower), for the first 2 i get: 568 and 1136, while for the second and third I get 1136 and 1704. It doesn't seem to matter which takes more screen or less, the frame.origin.y doesn't change. – asheyla Jan 20 '16 at 12:44