Prevent footer overlapping tableViewCell in UITableView - Swift

拜拜、爱过 提交于 2020-01-13 11:06:29

问题


I have this table view which works how i want it to however i have a problem where the footer overlap the cells in the table view as seen in

How can i prevent this? This is my code for the footer

   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)
  }

回答1:


Just add some padding to the bottom of the content so that it doesn't overlap with the footer:

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



回答2:


Try to override this method

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



回答3:


I know this is an old thread, so this is more so for others that encountered this (like myself).

The floating section footer is default behavior from my understanding. There are a couple of options that I can think of:

Provide your footer view with a background color (footerView.backgroundColor = UIColor.white) thus cleaning up the overlap.

or

Replace the section footer with a custom "Total Calories" cell that you add to the table after the last cell in that section, effectively acting like a footer cell.




回答4:


Sorry for the delay. If you have modified the standard contentInsetAdjustmentBehavior of the tableView, you must adjust the tableView contentInset property to take into account the total height of the views at the bottom of the UITableView, like the tab bar. If contentInsetAdjustmentBehavior is set to "automatic" (or you didn't change the default value), then set the clipsToBounds property of your footer view to true so that its child views cannot be painted outside the footer view layer's frame. That should solve your issue.




回答5:


Just set your label's background color to UIColor.white. and you are done !

label.backgroundColor = UIColor.white



回答6:


Of course it overlaps. This is how footers and header work in UITableViews. You can set the footerView.backgroundColor to UIColor.gray, for example, to make it look better.



来源:https://stackoverflow.com/questions/36391095/prevent-footer-overlapping-tableviewcell-in-uitableview-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!