我以编程方式创建了 UITableView。我没有使用故事板。我阅读并观看了大量关于如何创建可伸缩标题的教程,但它们都使用故事板。
据我了解,要实现拉伸效果,您必须将 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!
在我的代码中相当于什么?
这听起来可能很愚蠢,但我被困住了。帮助!