Ios 如何使uitableview的最后一行填充空白
我希望最后一个单元格/行(mapView)填充下面的空白。 我的tableviewcontroller代码Ios 如何使uitableview的最后一行填充空白,ios,swift,uitableview,Ios,Swift,Uitableview,我希望最后一个单元格/行(mapView)填充下面的空白。 我的tableviewcontroller代码 class CustomTableViewController: UITableViewController { let fakeArray = ["Row1","Row2","Row3"] // Array can contains more items override func viewDidLoad() { super.viewDidLoad(
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
我在另一个帖子中回答了类似的问题
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat{
if indexPath.row == 0 {
return firstRowHeight
}
else if indexPath.row == 1 {
return secondRowHeight
}
else if indexPath.row == 2 {
return thirdRowHeight
}
else {
return tableView.frame.size.height - firstRowHeight - secondRowHeight - thirdRowHeight
}
}
这个答案适用于可能有太多不同单元格和太多不同高度的情况,因此计算单元格0..n-1的高度不像@Mike Taverne的答案那么容易 在我的例子中,最后一个单元格应该垂直扩展,但是有一个最小的高度,所以看起来很好。在这种情况下,tableView是可滚动的 代码如下:
public function tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if item(for: indexPath) == "some condition" {
return xxx // some number
else if item(for: indexPath) == "some other condition" {
return yyy // some other height
} ...
else { // I KNOW THIS CELL WILL BE LAST
let minimalLastCellHeight = 200
guard let previousCell = tableView.visibleCells.last else {
return minimalLastCellHeight
}
let lowestYCoordinate = previousCell.frame.maxY // low corner Y coordinate of previous cell
return max(availableHeight - lowestYCoordinate, minimalLastCellHeight)
}
您可以增加最后一行的行高。我会将地图放在“表视图”的“页脚”视图中。您是否曾经在地图之前显示过如此多的行,以至于地图不可见?然后呢?@MikeTaverne是的,我会的。理想情况下,我想为这个案件的标准高度。当我有空的空间时,我想调整单元格的高度来填充这个空间