Swift2でTableViewを書くと一番上のCellとTableHeaderViewの間に以下のようなマージンができてしまします。
グレーのTableHeaderViewとピンクのCellの間の白い部分です。
このマージンはTableHeaderViewを消した場合でも存在しています。
どなたか原因を知っている方がいましたら教えていただけると幸いです。
ソースコード自体はストーリーボードからtableViewを接続してシンプルに記述しています。
swift
1import UIKit 2 3class MyPageViewController: UIViewController { 4 5 @IBOutlet weak var tableView: UITableView! 6 7 let menuList = ["プロフィールを編集する", "20Questionsに回答する", "アプリの設定", "興味カテゴリの編集"] 8 9 override func viewDidLoad() { 10 super.viewDidLoad() 11 12 tableView.delegate = self 13 tableView.dataSource = self 14 15 self.setupTableViewHeader() 16 } 17 18 override func didReceiveMemoryWarning() { 19 super.didReceiveMemoryWarning() 20 } 21 22 func setupTableViewHeader(){ 23 tableView.tableHeaderView = UIView(frame: CGRect(x: 0, y: 0, width: 0, height: 150)) 24 } 25} 26 27/** 28 * UITableView Datasource 29 */ 30extension MyPageViewController: UITableViewDataSource { 31 func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 32 return menuList.count 33 } 34 35 func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { 36 return UIView() 37 } 38 39 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 40 let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "Cell") 41 42 cell.textLabel?.text = menuList[indexPath.row] 43 return cell 44 } 45 46 func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? { 47 return nil 48 } 49} 50 51 52/** 53 * UITableView Delegate 54 */ 55extension MyPageViewController: UITableViewDelegate { 56 func tableView(table: UITableView, didSelectRowAtIndexPath indexPath:NSIndexPath) { 57 print(menuList[indexPath.row]) 58 } 59}
またsetupTableViewHeader()関数の中身を以下のように
swift
1 func setupTableViewHeader(){ 2 let headerView: MyPageHeaderView = UINib(nibName: "MyPageHeader", bundle: nil).instantiateWithOwner(self, options: nil)[0] as! MyPageHeaderView 3 headerView.bounds = CGRect(x: 0, y: 0, width: 0, height: 100) 4 tableView.tableHeaderView = headerView 5} 6
XiBから読み込む形に書き換えると
のように一番うえのCellがTableHeaderViewの下に潜り込んでしまいます。
これの原因も原因がわかる方がいればご回答いただけると幸いです。
MyPageHeaderViewの詳細
swift
1class MyPageHeaderView: UIView { 2 @IBOutlet weak var profileImageView: UIImageView! 3 4 override func awakeFromNib() { 5 self.setupProfileImage() 6 } 7 8 func setupProfileImage(){ 9 profileImageView.layer.cornerRadius = 60 10 profileImageView.layer.masksToBounds = true 11 profileImageView.layer.borderColor = UIColor.redColor().CGColor 12 profileImageView.layer.borderWidth = 2 13 } 14} 15
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/03/09 07:38 編集
2016/03/09 08:14
2016/03/09 08:28
2016/03/09 08:41
2016/03/10 02:59
2016/03/10 03:57
2016/03/10 06:21
2016/03/10 06:23
2016/03/10 08:23
2016/03/10 15:02