【问题标题】:Issue with section header and table view cell in UITableViewControllerUITableViewController 中的部分标题和表格视图单元格的问题
【发布时间】:2016-11-29 13:30:47
【问题描述】:

我做了一个使用 UITableviewController 而不是滚动视图创建屏幕的实验,原因是屏幕中的字段可能会发生巨大变化,这就是我这样做的原因

屏幕:

所有这些单元格都是静态单元格。

部分标题代码

let backgroundImage = UIImage(named: "main_bg")
    let imageView = UIImageView(image: backgroundImage)
    self.tableView.backgroundView = imageView

    header.backgroundColor = UIColor.clear
    let lblheader : appLabel = appLabel(frame: CGRect(x: 0, y: 20, width: UIScreen.main.bounds.width, height: header.frame.height - 20))
    lblheader.text = "SIGN UP"
    lblheader.textFontsize = 14
    lblheader.textFontType = 2
    lblheader.textFontColor = 1
    lblheader.textAlignment = .center
    header.addSubview(lblheader)

    let btnback : appButton = appButton(frame: CGRect(x: 0, y: lblheader.frame.origin.y, width: 50, height: lblheader.frame.height))
    btnback.backgroundColor = UIColor.clear
    btnback.setImage(UIImage(named: "back_arrow"), for: .normal)
    btnback.addTarget(self, action: #selector(self.btnBackTapped), for: .touchUpInside)
    header.addSubview(btnback)

现在当我滚动表格时,它看起来如下:

我想滚动标题部分下表格的静态单元格。 我知道我也可以使用 UIViewController 的组合,它包含 Container 并代表 Table view 控制器(参考:See this i used it already) 如果有除此之外的任何解决方案。 请指导我!

谢谢

【问题讨论】:

  • 您需要一个标题,不要添加 UIView 并将 tableView 放在其下。很简单;)

标签: ios swift uitableview header


【解决方案1】:

或者,您可以使用 UITableView 的 tableHeaderView 而不是 UITableView's viewForHeaderInSection,因为节标题 WILL 始终会粘在 tableview 的顶部,除非您进行一些简单的修改

如果您使 SectionHeader 不透明而不是透明/半透明,您就会明白。尝试制作一个自定义项目(测试项目),在其中创建一个 TableView,其中包含许多具有不同标题的部分

不创建示例项目的完美示例是 iPhone 中的 Phone 应用程序(绿色应用程序),其中 Letters 是部分标题,滚动时它们会粘在屏幕顶部

这是 Swift 2.3

中的示例
let someHeader = NSBundle.mainBundle().loadNibNamed("", owner:self, options nil!)!.first as! SomeView

//this is not a real function just what you wanna do stuff
someHeader.customizeIfNeeded ... yadda yadda set frame assign values add targets etc


tableView.tableHeaderView = someHeader
tableView.reloadData()

【讨论】:

    【解决方案2】:

    使用UIViewController而不是UITableViewController并在tableView顶部添加一个UIView作为标题

    参考这个maintain the header of a tableView fixed

    只是一个简单的方法,希望这会有所帮助

    如果您必须使用“UITableViewController”,请查看以下解决方案

    UITableViewController (static cells/keyboard handling) and have a fixed header

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-26
      • 1970-01-01
      • 2021-11-18
      • 1970-01-01
      相关资源
      最近更新 更多