我已经以编程方式创建了UITableView。我不使用故事板。 我阅读并观看了大量关于如何创建可伸缩标题的教程,但所有这些教程都使用了Storyboard。以编程方式创建的UITableView的伸缩标题 - Swift
据我所知,为了实现有弹性的效果,您必须将tableViewHeader添加为子视图,然后使用.sendSubviewToBack将其移动到后面,然后应用其余的逻辑。
但我不知道什么必须发送到子视图时编程创建标题。当使用故事板时,您将自定义标头类分配给一个变量,然后将其添加到子视图中。所以这里是我的TableViewcontroller:
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
什么是等同的在我的代码headerView:ArticleHeaderView!
?
这可能听起来很愚蠢,但我卡住了。帮帮我!