开发者

Find height of the empty space in a UITableView

开发者 https://www.devze.com 2023-04-12 11:50 出处:网络
In my UITableView I need to position an UIView at the footer of the table and outside of the screen. For example, the \"Push to refresh\" functionality (like Twitter app) is at the begging of the tabl

In my UITableView I need to position an UIView at the footer of the table and outside of the screen. For example, the "Push to refresh" functionality (like Twitter app) is at the begging of the table and outside the screen, I need the same but at the bottom.

If I have enough rows with a total height bigger than the screen height I don't have any problem, but if I haven't any rows I can't position 开发者_如何转开发muy view.

So I need to add a blank cell (custom cell) to my table to fill the empty space between the last cell and the bottom of the screen, how can I do it? How can I Know the height of the empty space?

Thank you very much form your help.


Instead of a blank cell, why not use the same table footer view and just make it large enough to contain that empty space. It seems much easier.

Now, the only solution I can think of right now is to manually calculate the height of your content. For this, I think you can use a trick and just call


CGRect lastRowFrame = [tableView rectForRowAtIndexPath: last_row_index_path];

As the documentation says, this will get you the frame for the last row and with this you can calculate the difference between frame.size.height and the last row frame. Something like this:


CGFloat emptySpaceHeight = tableView.frame.size.height - (lastRowFrame.origin.y + lastRowFrame.size.height);

This will be the empty space you need to add to the footer view.

Oh, and one last trick to get that effect: make sure you use tableView.contentInset to set the bottom inset to minus "Push to refresh" message height. This will make the tableview bounce and rest with the message out of the screen.

Right now I can't think of a better way to achieve what you want. So, hope this helps.


Swift version

        lastRowFrame = tableView.rectForRow(at: indexPath)
        let emptySpaceHeight = tableView.frame.size.height - (lastRowFrame.origin.y + lastRowFrame.size.height)


You can try adding this into heightforrowat function, this lets the bottom cell to have the size for the missing space between the last cell and bottom of the screen.

    if indexPath == (lastIndexPath) {
        var currentCellsHeight: CGFloat = 0
        let minimumFooterCellHeight = 50 // Whatever you want the minimum to be

        for visibleCell in tableView.visibleCells {
            currentCellsHeight += visibleCell.frame.height
        }

        if let footerCell = tableView.cellForRow(at: indexPath) {
            currentCellsHeight -= footerCell.frame.height
        }
        if self.view.frame.height - currentCellsHeight < minimumFooterCellHeight {
            return minimumFooterCellHeight
        } else {
            return self.view.frame.height - currentCellsHeight
        }
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消