简体   繁体   English

UiView固定在UiTableViewController的顶部

[英]UiView fixed on top of UiTableViewController

I need to put an UIView fixed on top of UITableViewController (like a header). 我需要将固定的UIView放在UITableViewController的顶部(如标头)。 I've tried this: 我已经试过了:

override func scrollViewDidScroll (scrollView: UIScrollView)  {
     var fixedFrame: CGRect = self.uiTopView.frame;
     fixedFrame.origin.y = scrollView.contentOffset.y;
     self.uiTopView.frame = fixedFrame;
}

But it does not work and I don't know why. 但这不起作用,我也不知道为什么。 Someone have any idea? 有人知道吗

This can not be done, one way to accomplish this is to add the UITableViewController inside UIContainerView So the structure will be as follows: 无法做到这一点,一种实现方法是在UIContainerView内添加UITableViewController,因此结构如下:

ViewController1 contains a UIContainerView this container view has embedded segue to your tableViewController. ViewController1包含一个UIContainerView此容器视图已将segue嵌入到tableViewController中。

Then you can add the view to the ViewController1. 然后,您可以将视图添加到ViewController1。

Why do you actually use UITableViewController instead of UIViewController with a tableView inside? 为什么您实际上在内部使用tableView而不是UITableViewController而不是UIViewController?

Maybe you should add your header view first then add you tableview depending on the header's frame. 也许您应该先添加标题视图,然后再根据标题框架添加表视图。

for example: `import UIKit 例如:`import UIKit

class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource { var fixedLabel : UILabel! 类ViewController:UIViewController,UITableViewDelegate,UITableViewDataSource {var fixedLabel:UILabel! var tableView : UITableView! var tableView:UITableView!

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
     self.tableView.frame = CGRectMake(0, self.fixedLabel.frame.maxY, self.view.frame.width, self.view.frame.height-70)
     self.fixedLabel.frame = CGRectMake(0,0,self.view.bounds.width,70)
}

override func viewDidLoad() {
    super.viewDidLoad()

    self.fixedLabel = UILabel()
    self.fixedLabel.backgroundColor = UIColor.blueColor()
    self.fixedLabel.text = "This is a fixedLabel"
    self.fixedLabel.textAlignment = .Center

    self.tableView = UITableView()
    self.tableView.delegate = self
    self.tableView.dataSource = self

    self.view.addSubview(fixedLabel)
    self.view.addSubview(tableView)

}

 func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}

 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {


   var cell : UITableViewCell? = tableView.dequeueReusableCellWithIdentifier("cell")

    if cell == nil {
        cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "cell")
    }

    cell?.textLabel?.text = "Your text"
    return cell!
}

 func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 3
}

} ` }`

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

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