iOS   发布时间:2022-03-30  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ios – 如何使最后一行uitableview填充空白区域大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我想最后一个单元格/行(mapView)填充下面的空白区域.

ios – 如何使最后一行uitableview填充空白区域

我的tableviewcontroller代码

class CustomTableViewController: UITableViewController {

    let fakeArray = ["Row1","Row2","Row3"] // Array can contains more items

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(tableView: UITableView,numberOfRowsInSection section: int) -> Int {
        return fakeArray.count+1
    }

    override func tableView(tableView: UITableView,cellForRowATindexPath indexPath: NSIndexPath) -> UITableViewCell {

        if(indexPath.row != fakeArray.count){
            let cellIdentifier = "TestTableViewCell"
            let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier,forIndexPath: indexPath) as! TestTableViewCell
            cell.rowName.text = fakeArraY[indexPath.row]
            return cell
        }else{
            let cellIdentifier = "MapTableViewCell"
            let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier,forIndexPath: indexPath) as! MapTableViewCell
            return cell
        }

    }

    override func tableView(tableView: UITableView,heightForRowATindexPath indexPath: NSIndexPath) -> CGFloat {
        if(indexPath.row != fakeArray.count){
            return 44
        }else{
            return 80
        }
    }

}

有时我会在地图前显示更多行.所以下面的空白区域会有所不同.其中空白空间也会因iPhone屏幕尺寸而异.

解决方法

您需要确定您有多少空白空间,然后将地图单元格的高度设置为该空间.

首先,将表视图本身的顶部和底部约束到superview,以确保它占据整个屏幕.

表格视图的可见高度为:

self.tableView.bounds.size.height

在heightForRowATindexPath中,计算地图单元格上方单元格的高度,并返回该视图与表格视图的可见高度之间的差异:

let otherCellsHeight: CGFloat = fakeArray.count * 44

    return self.tableView.bounds.size.height - otherCellsHeight

当然,您还应检查地图单元的计​​算高度是否为负值或小于某个最小高度,以确保安全:

let availableSpace = self.tableView.bounds.size.height - otherCellsHeight

    return (availableSpace > 80) ? availableSpace : 80

大佬总结

以上是大佬教程为你收集整理的ios – 如何使最后一行uitableview填充空白区域全部内容,希望文章能够帮你解决ios – 如何使最后一行uitableview填充空白区域所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。