2015-08-12 14 views
9

Tôi cần thêm tiêu đề tùy chỉnh để bàn của tôiSwift - cách tạo tiêu đề tùy chỉnh cho UITableView?

tôi cố gắng này

func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { 

    let view = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.size.width, height: 18)) 
    let label = UILabel(frame: CGRect(x: 20, y: 20, width: 50, height: 50)) 
    label.text = "TEST TEXT" 
    label.textColor = UIColor.whiteColor() 

    self.view.addSubview(view) 

    return view 
} 

nhưng điều này không làm việc, tôi thấy không có gì trên bàn

Tôi đang làm gì sai? Hoặc có thể có một cách khác?

+3

Thêm 'view' cho 'self.view' là không cần thiết. Nó sẽ bị xóa khỏi đó và được thêm vào chế độ xem bảng. – Cyrille

+0

@Cyrille Tôi không chắc tại sao bạn cho rằng khung nhìn sẽ bị xóa sau khi nó được thêm vào mảng subviews theo lập trình, nhưng có vẻ như nó sẽ được thêm vào hệ thống phân cấp ở hai vị trí khác nhau, điều này sẽ dẫn đến không xác định hành vi. – jlehr

+0

Không. Chế độ xem chỉ có thể có một người giám sát tại một thời điểm.Nếu bạn thêm chế độ xem ở bất kỳ đâu, trước tiên nó sẽ bị xóa khỏi vị trí trước đó. – Cyrille

Trả lời

12

Bạn có thiết lập chiều cao tiêu đề phần trong viewDidLoad?

self.tableView.sectionHeaderHeight = 70 

Thêm vào đó bạn nên thay thế

self.view.addSubview(view) 

bởi

view.addSubview(label) 

Cuối cùng, bạn phải kiểm tra khung của bạn

let view = UIView(frame: CGRect.zeroRect) 

và cuối cùng là màu văn bản mong muốn vì nó dường như hiện tại là màu trắng trên nền trắng.

+0

Cảm ơn bạn rất nhiều! Đặc biệt là để nhận xét về ViewDidLoad. Bây giờ nó hoạt động tuyệt vời –

7

thêm label để subview tùy chỉnh view, không cần self.view.addSubview(view), vì viewForHeaderInSection bù lại UIView

view.addSubview(label) 
+0

Bạn cần phải thêm nhãn, có; nhưng không cần thêm 'view' vào' self.view'. Nó sẽ bị xóa khỏi đó và được thêm vào chế độ xem bảng. – Cyrille

4

Nếu bạn đang sử dụng ô tùy chỉnh làm tiêu đề, hãy thêm phần sau.

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { 

     let headerView = UIView() 
     let headerCell = tableView.dequeueReusableCell(withIdentifier: "customTableCell") as! CustomTableCell 
     headerView.addSubview(headerCell) 
     return headerView 
    } 

Nếu bạn muốn có chế độ xem đơn giản, hãy thêm phần sau đây.

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { 
let headerView:UIView = UIView() 
    return headerView 
} 
4

Nếu bạn sẵn sàng để sử dụng tiêu đề bảng tùy chỉnh như tiêu đề bảng, hãy thử các sau ....

cập nhật cho nhanh 3,0

Bước 1

Tạo UITableViewHeaderFooterView cho tiêu đề tùy chỉnh ..

import UIKit 

class MapTableHeaderView: UITableViewHeaderFooterView { 

    @IBOutlet weak var testView: UIView! 

} 

Bước 2

Thêm tiêu đề tùy chỉnh để UITableView

override func viewDidLoad() { 
      super.viewDidLoad() 

      tableView.delegate = self 
      tableView.dataSource = self 

      //register the header view 

      let nibName = UINib(nibName: "CustomHeaderView", bundle: nil) 
      self.tableView.register(nibName, forHeaderFooterViewReuseIdentifier: "CustomHeaderView") 


    } 

    extension BranchViewController : UITableViewDelegate{ 

    } 

    extension BranchViewController : UITableViewDataSource{ 

     func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { 
      return 200 
     } 

     func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { 
      let headerView = self.tableView.dequeueReusableHeaderFooterView(withIdentifier: "CustomHeaderView") as! MapTableHeaderView 

      return headerView 
     } 

     func tableView(_ tableView: UITableView, numberOfRowsInSection section: 

    Int) -> Int { 
      // retuen no of rows in sections 
     } 

     func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
      // retuen your custom cells  
     } 

     func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 

     } 

     func numberOfSections(in tableView: UITableView) -> Int { 
      // retuen no of sections 
     } 

     func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 
      // retuen height of row 
     } 


    } 
0

này làm việc cho tôi - Swift 3

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { 

     let headerCell = tableView.dequeueReusableCell(withIdentifier: "customTableCell") as! CustomTableCell 
     return headerCell 
    } 
Các vấn đề liên quan