tableView의 헤더는 위쪽이라든지 아래로 굴러갈 때 고정되어 있고 위에서 만든 샘플은 위쪽입니다.
12679 단어 Swift
나는 이런 디자인이 매우 흔하다고 생각하지만 어떻게 해야 좋을지 모르겠다.나는 이것이 간단한 실현 방식이라고 생각한다.
처음에는 UItableView
tableHeaderView로 어떻게 실시하려고 했지만 전혀 몰랐어요.그걸 사용하지 않고 "단순히 테이블 뷰에 오리지널 UIView를 올리는 방법"입니다.요점은 '스크롤할 때의 좌표 계산을 통해 눈썹을 고정시키는 것' 이다.
ViewController.swift
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var myTableView: UITableView!
var myItems: NSMutableArray = []
var myHeaderView: UIView!
var statusBarHeight: CGFloat!
var displayWidth: CGFloat!
var displayHeight: CGFloat!
/*
ロード後
*/
override func viewDidLoad() {
super.viewDidLoad()
self.setItem()
statusBarHeight = UIApplication.sharedApplication().statusBarFrame.height
displayWidth = self.view.frame.width
displayHeight = self.view.frame.height
// テーブル
myTableView = UITableView(frame: CGRect(x: 0, y: statusBarHeight, width: displayWidth, height: displayHeight))
myTableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "MyCell")
myTableView.dataSource = self
myTableView.delegate = self
myTableView.contentInset.top = 100 //(★..コンテンツをヘッダー高さ分下げる)
self.view.addSubview(myTableView)
// オリジナルヘッダービューを作成
myHeaderView = UIView(frame: CGRect(x: 0, y: -100, width: displayHeight, height: 100)) //(★..コンテンツの上にヘッダーを配置)
myHeaderView.backgroundColor = UIColor.greenColor()
myHeaderView.alpha = 0.5
myTableView.addSubview(myHeaderView)
let myLabel = UILabel(frame: CGRect(x: 0, y: 0, width: displayWidth, height: 50))
myLabel.text = "高さ100のオリジナルヘッダービュー"
myLabel.font = UIFont.systemFontOfSize(12)
myLabel.textAlignment = .Center
myHeaderView.addSubview(myLabel)
}
/*
アイテムをただ用意するだけ
*/
func setItem() {
for index in 1...18 {
self.myItems.addObject("Item\(index)")
}
}
/*
テーブル行数
*/
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.myItems.count
}
/*
テーブルセルの設定
*/
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("MyCell", forIndexPath: indexPath)
cell.textLabel?.text = self.myItems[indexPath.row] as? String
return cell
}
/*
スクロール時
*/
func scrollViewDidScroll(scrollView: UIScrollView) {
// 下に引っ張ったときは、ヘッダー位置を計算して動かないようにする(★ここがポイント..)
if scrollView.contentOffset.y < -100 {
self.myHeaderView.frame = CGRect(x: 0, y: scrollView.contentOffset.y, width: self.displayWidth, height: 100)
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Reference
이 문제에 관하여(tableView의 헤더는 위쪽이라든지 아래로 굴러갈 때 고정되어 있고 위에서 만든 샘플은 위쪽입니다.), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/mochizukikotaro/items/f48559630a639e7d467b텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)