How to determine if the last UITableView cell is displayed on the screen

I use the following code to determine if the last window on the screen is displayed:

override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    if indexPath.row == collection.count - 1 {
        print("last cell is seen!!")
        print(cell.frame)
    }
}

This works on small screens where the user has to scroll to get to the desired cell. However, on large screens where the entire table is visible, this method does not work. Is there a way to determine the distance between the bottom of the screen to the last cell in UITableView?

Any help would be greatly appreciated. Thank.

Edit: this is what I'm trying to accomplish.

if (the last cell && is visible more than 100dp from the bottom of the screen) {display a fixed button at the bottom of the screen} else {add a button to the table view footer so that the user can scroll down to the button}

+4
3

, ( ).

,

guard let lastCell = tableView.cellForRow(at: IndexPath(row: tableView.numberOfRows(inSection: 0)-1, section: 0)) else {
   return
}

let lastCellBottomY = lastCell.frame.maxY

, tableView, :

let delta = tableHeight - lastCellBottomY

, :

if delta > 55 { 
   // ...show button
} else {
   // ...hide button
} 

(, View ), .

:

https://www.dropbox.com/s/7wpwv6737efnt5v/ButtonTable.zip?dl=0

, :)

+1

, , .

func isCellVisible(section:Int, row: Int) -> Bool {
    guard let indexes = self.indexPathsForVisibleRows else {
        return false
    }
    return indexes.contains {$0.section == section && $0.row == row }
}  }

let lastCell = collection.count - 1
let result = isCellVisible(section:"Your Section Number", row: lastCell)
+1

try it! Hope to help you.

func scrollViewDidScroll(_ scrollView: UIScrollView) {

    let contentOffsetMaxY: Float = Float(scrollView.contentOffset.y + scrollView.bounds.size.height)
    let contentHeight: Float = Float(scrollView.contentSize.height)

    let ret = contentOffsetMaxY > contentHeight - 100
    if ret {
        print("testButton is show");
    }else{
        print("testButton is hidden");
    }
}
+1
source

Source: https://habr.com/ru/post/1685639/


All Articles