防止 UITableView 中的页脚重叠 tableViewCell - Swift

问题描述 投票:0回答:8

我有这个表格视图,它可以按照我想要的方式工作,但是我有一个问题,即页脚与表格视图中的单元格重叠,如

所示

我怎样才能防止这种情况发生?这是我的页脚代码

   func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {

    let footerView = UIView(frame: CGRectMake(0, 0, tableView.frame.size.width, 40))
    //    self.myTableView.tableFooterView = footerView;
    let sectionString = Array(foodArray.keys)[section]


      for value in caloriesArray[sectionString]! {
        calories += value
       }
       totalCalories += calories

      print(calories)
      print(totalCalories)
      let label = UILabel(frame: CGRectMake(footerView.frame.origin.x - 15, footerView.frame.origin.y, footerView.frame.size.width, 20))

       label.textAlignment = NSTextAlignment.Right

       label.text = "Total Calories: \(calories) "

      footerView.addSubview(label)
       calories = 0
  return footerView
  }

 func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {

  return 20.0
}

@IBAction func addFoodTapped(sender: AnyObject) {


}


func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {


    let sectionString = Array(foodArray.keys)[indexPath.section]

    foodArray[sectionString]?.removeAtIndex(indexPath.row)
    caloriesArray[sectionString]?.removeAtIndex(indexPath.row)
    print(foodArray)
   viewDidAppear(true)
  }
ios swift uitableview
8个回答
6
投票

你可以这样做,只需制作样式

Grouped
,如下所示:


2
投票

只需在内容底部添加一些填充,使其不与页脚重叠:

self.tableView.contentInset = UIEdgeInsetsMake(0, 0, FOOTER_HEIGHT, 0)

0
投票

我知道这是一个旧线程,所以对于遇到此问题的其他人(比如我自己)来说更是如此。

根据我的理解,浮动部分页脚是默认行为。我能想到几个选项:

为页脚视图提供背景颜色(footerView.backgroundColor = UIColor.white),从而清理重叠。

将部分页脚替换为自定义的“总卡路里”单元格,该单元格添加到该部分最后一个单元格之后的表格中,实际上就像页脚单元格一样。


0
投票

抱歉耽搁了。如果您修改了 tableView 的标准 contentInsetAdjustmentBehavior,则必须调整 tableView contentInset 属性以考虑 UITableView 底部视图(如选项卡栏)的总高度。如果 contentInsetAdjustmentBehavior 设置为“自动”(或者您没有更改默认值),则将页脚视图的 ClipsToBounds 属性设置为 true,以便其子视图无法绘制在页脚视图图层框架之外。那应该可以解决你的问题。


0
投票

我的桌面视图顶部有一个“粘滞/悬停”按钮,也遇到了类似的问题。它与我的部分标题重叠。 利用

contentInset
提供填充解决了一半的问题。它显示了标题,但标题现在与“顶部”单元格重叠。

tableView 上下翻转,因为它是一个

Chat
tableView 并且需要从底部填充单元格。

我发现我没有使用

viewForFooterInSection
,而是使用了整个 tableView 的主页脚,如下所示:

    self.tableView.contentInset = .init(bottom: 50) // height of sticky overlay button
    let header = self.getHeader()
    self.tableView.tableFooterView = header

但是,这将使标题在覆盖按钮下滚动,这在我的情况下是需要的,换句话说,标题不会粘在

contentInset
上。


-1
投票

尝试重写此方法

override func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
    return 44.0
}

-2
投票

当然是重叠的。这就是页脚和页眉在

UITableView
中的工作方式。例如,您可以将
footerView.backgroundColor
设置为
UIColor.gray
,使其看起来更好。


-2
投票

只需将标签的背景颜色设置为

UIColor.white.
即可完成!

label.backgroundColor = UIColor.white
© www.soinside.com 2019 - 2024. All rights reserved.