Ios 以编程方式创建的UITAbleView的弹性标题-Swift
我以编程方式创建了UITableView。我不使用故事板。 我阅读并观看了大量关于如何创建弹性页眉的教程,但都使用了故事板 据我所知,要实现拉伸效果,必须将tableViewHeader添加为子视图,然后使用.sendSubviewToBack将其移到后面,然后应用其余逻辑 但是,当以编程方式创建标头时,我无法确定必须向子视图发送什么。使用故事板时,将自定义标题类指定给变量,然后将其添加到子视图。这是我的TableViewcontroller:Ios 以编程方式创建的UITAbleView的弹性标题-Swift,ios,swift,uitableview,uitableviewsectionheader,Ios,Swift,Uitableview,Uitableviewsectionheader,我以编程方式创建了UITableView。我不使用故事板。 我阅读并观看了大量关于如何创建弹性页眉的教程,但都使用了故事板 据我所知,要实现拉伸效果,必须将tableViewHeader添加为子视图,然后使用.sendSubviewToBack将其移到后面,然后应用其余逻辑 但是,当以编程方式创建标头时,我无法确定必须向子视图发送什么。使用故事板时,将自定义标题类指定给变量,然后将其添加到子视图。这是我的TableViewcontroller: import UIKit private let
import UIKit
private let reuseIdentifier = "contentCellId"
private let headerIdentifier = "headerCellId"
class ItemDetailViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
tableView.registerClass(ItemDetailsContentCell.self, forCellReuseIdentifier: reuseIdentifier)
tableView.registerClass(ItemDetailsHeaderCell.self, forHeaderFooterViewReuseIdentifier: headerIdentifier)
let headerView = tableView.tableHeaderView
print(headerView) // returns NIL
tableView.separatorStyle = .None
// TableView heights
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 500.0
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return 1
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(reuseIdentifier, forIndexPath: indexPath) as! ItemDetailsContentCell
// Configure the cell...
return cell
}
override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerView = tableView.dequeueReusableHeaderFooterViewWithIdentifier(headerIdentifier)
return headerView
}
override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
let cell = tableView.dequeueReusableHeaderFooterViewWithIdentifier(headerIdentifier) as! ItemDetailsHeaderCell
let width = view.frame.width
let imageRatio = (cell.itemImage.image?.size.height)! / (cell.itemImage.image?.size.width)!
let newHeight = width * imageRatio
return newHeight
}
// override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
// return UITableViewAutomaticDimension
// }
//
// override func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
// return 500
// }
}
没什么特别的。
下面是我试图复制的代码示例:
@IBOutlet weak var tableView:UITableView!
@IBOutlet weak var headerView:ArticleHeaderView!
var article: Article?
// Header view configuration
private var defaultTableHeaderHeight:CGFloat = 250.0
private var lastContentOffset:CGFloat = 0.0
private let defaultHeaderImageName = "bg-pattern"
override func viewDidLoad() {
super.viewDidLoad()
tableView.separatorStyle = .None
tableView.rowHeight = UITableViewAutomaticDimension
// Add the header view to the table view background
defaultTableHeaderHeight = headerView.frame.size.height
lastContentOffset = -defaultTableHeaderHeight
print(defaultTableHeaderHeight)
print(tableView.tableHeaderView!.frame.height)
tableView.tableHeaderView = nil
tableView.addSubview(headerView)
tableView.sendSubviewToBack(headerView)
tableView.contentInset = UIEdgeInsets(top: defaultTableHeaderHeight, left: 0, bottom: 0, right: 0)
tableView.contentOffset = CGPoint(x: 0, y: -defaultTableHeaderHeight)
// Disable content inset adjustment
self.automaticallyAdjustsScrollViewInsets = false
什么是头视图的等价物代码>在我的代码中
听起来可能很傻,但我被卡住了。救命啊 您可以在这里看到一个示例: 本文作者讨论了如何创建自定义标题视图,并使用代码中的约束使用它,以及如何使用UITableView继承的scrollView的委托应用转换 他讨论了向下和向上滚动以使headerView变小或变大
func animateHeader() {
self.headerHeightConstraint.constant = 150
UIView.animateWithDuration(0.4, delay: 0.0,
usingSpringWithDamping: 0.7, initialSpringVelocity: 0.5,
options: .CurveEaseInOut, animations: {
self.view.layoutIfNeeded()
}, completion: nil)
}
func scrollViewDidEndDragging(scrollView: UIScrollView, willDecelerate decelerate: Bool) {
if self.headerHeightConstraint.constant > 150 {
animateHeader()
}
}
func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
if self.headerHeightConstraint.constant > 150 {
animateHeader()
}
}