プログラムでUITableViewを作成しました。私はストーリーボードを使用していません。 私はストレッチ・ヘッダーを作成する方法に関する多数のチュートリアルを読んで見ましたが、それらのすべてはストーリーボードを使用しています。プログラムで作成された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!
の同等何?
馬鹿に聞こえるかもしれないが、私は立ち往生している。助けて!