简体   繁体   English

UITableViewController中的节标题和表格视图单元的问题

[英]Issue with section header and table view cell in UITableViewController

I've done an experiment of creating a screen with the use of UITableviewController instead of using scroll view reason is the fields in screen may get dramatically change that's why I do that 我做了一个使用UITableviewController而不是使用滚动视图创建屏幕的实验,原因是屏幕中的字段可能会发生巨大变化,这就是我这样做的原因

Screen : 屏幕: 在此处输入图片说明

All of those cells are static cells. 所有这些单元都是静态单元。

section header code 节头代码

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)

Now when I scroll the table It looks as below: 现在,当我滚动表格时,它看起来如下: 在此处输入图片说明

I want to scroll static cell of the table under header section. 我想在标题部分下滚动表格的静态单元格。 I know I can also use combination of UIViewController which contains Container and represents the Table view controller (Ref : See this i used it already ) If any solutions other than that. 我知道我也可以使用UIViewController的组合,该组合包含Container并代表Table视图控制器( 请参阅参考资料,我已经使用过 )是否有其他解决方案。 Please guide me! 请指导我!

Thank you 谢谢

alternatively you can use UITableView's tableHeaderView instead of using UITableView's viewForHeaderInSection because Section Headers WILL always stick to the top of the tableview unless otherwise if you do some simple hacks 另外,您也可以使用UITableView的tableHeaderView而不是UITableView's viewForHeaderInSection因为节标题始终UITableView's viewForHeaderInSection在tableview的顶部,除非您进行一些简单的修改

If you make your SectionHeader opaque and not transparent/translucent you'll understand. 如果使SectionHeader不透明并且不透明/半透明,您将会理解。 Try making a Custom Project (test project) where you create a TableView with a lot of Sections With Different Headers 尝试制作一个自定义项目(测试项目),在其中创建一个TableView并包含许多带有不同标题的节

Perfect example without creating the Sample Project would be the Phone Application (the green one) in your iPhone where the Letters are Section Headers and they stick at the top of your screen when scrolling 没有创建示例项目的完美示例是iPhone中的Letters Phone应用程序(绿色的),其中“ Letters为“节头”,并且在滚动时它们停留在屏幕顶部

This is an example in Swift 2.3 这是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()

Use UIViewController instead of UITableViewController and add a UIView as header in top of tableView 使用UIViewController代替UITableViewController并将UIView作为表头顶部的标题添加

Refer this maintain the header of a tableView fixed 参照此内容维护tableView的标头固定

Just an easy approach, hope this helps 只是一个简单的方法,希望对您有所帮助

If you have to use 'UITableViewController' look at below solution also 如果您必须使用“ UITableViewController”,请参见以下解决方案

UITableViewController (static cells/keyboard handling) and have a fixed header UITableViewController(静态单元格/键盘处理)并具有固定的标头

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM