简体   繁体   中英

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

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. 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

If you make your SectionHeader opaque and not transparent/translucent you'll understand. Try making a Custom Project (test project) where you create a TableView with a lot of Sections With Different Headers

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

This is an example in 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

Refer this maintain the header of a tableView fixed

Just an easy approach, hope this helps

If you have to use 'UITableViewController' look at below solution also

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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