この「週」ビューがテーブルビューの一部であるかどうかはわかりません。それは本当に問題ではありませんが、私は本当にこのようなものをどのように実装できるのか不思議です。
確かに可能性があります!
お客様の実装は、UICollectionView
とUITableView
のどちらを使用しているかによって異なる場合があります。
UITableView
の場合、水平スクロールページを含むサンプル内のセクションはUITableViewHeader
です。 「週」ビューを含むスティッキー領域はUITableHeaderFooterView
のカスタムで、tableView:viewForHeaderInSection:
で定義します。
UICollectionView
の場合、少し手作業が必要です。アイデアは基本的に同じです.1つのセクションには、コレクションビューのレイアウト定義ごとにナビゲーションに固執するヘッダ(UICollectionElementKindSectionHeader
)が含まれています。そのことについては、hereとhereをご覧ください。
編集:ここ遊び場に評価するために、ラフな例です。
import UIKit
import Foundation
import XCPlayground
class StickyTableViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var tableView: UITableView!
var items = ["1",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"10",
"11",
"12",
"13",
"14",
"15",
"16",
"17",
"18",
"19",
"20",
"21",
"22",
"23",
"24",
"25"
]
let tableViewHeader = UIView()
override func viewDidLoad() {
super.viewDidLoad()
self.tableView = UITableView(frame: self.view!.frame, style: .Plain)
self.tableView.delegate = self
self.tableView.dataSource = self
self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "TableViewCell")
tableViewHeader.frame = CGRectMake(0, 0, self.tableView.frame.width, 300)
tableViewHeader.backgroundColor = UIColor.blueColor()
self.tableView.tableHeaderView = tableViewHeader
self.view.addSubview(self.tableView)
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
switch section {
case 0:
return 0
default:
return items.count
}
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 2
}
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerView = UIView(frame: CGRectMake(0, 0, self.tableView.frame.width, 40))
headerView.backgroundColor = UIColor.grayColor()
let headerLabel = UILabel(frame: headerView.frame)
headerLabel.text = "Label"
headerLabel.textAlignment = .Center
headerView.addSubview(headerLabel)
switch section {
case 0:
return nil
case 1:
return headerView
default:
return headerView
}
}
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
switch section {
case 0:
return nil
default:
return " "
}
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("TableViewCell", forIndexPath: indexPath)
cell.textLabel?.text = "\(self.items[indexPath.row])"
return cell
}
}
let rootViewController = StickyTableViewController()
let navigationController = UINavigationController(rootViewController: rootViewController)
let page = XCPlaygroundPage.currentPage
page.liveView = navigationController.view
UITableHeaderFooterViewはスクロールするとどのように上にスティックされますか?このデフォルトの動作ですか? – edwardmp
これは、複数のセクションを持つ 'UITableView'のデフォルト動作です。テーブルにセクションが1つしかないと仮定すると、最初のセクションを空[1]と宣言し、ヘッダーを持たない[2]ように強制することができます。 [1]「numberOfRowsInSection」を参照してください。 [2] 'viewForHeaderInSection'を参照してください。 –
@chrisdoukas例のおかげで。しかし、私が望むのは、たとえあなたが他のセクションをスクロールしたとしても、このセクションヘッダーが一番上に留まることです。 – edwardmp