0

我試圖學習如何以編程方式創建不同的UI元素。我面對我的UITableView以下問題..部分標題不顯示在程序化UITableView

我有2個.swift文件,一方面,我們有..

 struct SettingsView { 

     let settingsCustomTable: UITableView = { 

      let aTable = UITableView() 
       aTable.sectionHeaderHeight = 42 
       aTable.tableFooterView = UITableViewHeaderFooterView(frame:CGRect(x:0, 
                        y:0, 
                        width:aTable.frame.width, 
                        height:0)) 

       aTable.register(SettingsCustomHeader.self, forHeaderFooterViewReuseIdentifier:"customHeader") 
       aTable.register(SettingsCustomCell.self, forCellReuseIdentifier:"customCell") 

      return aTable 
     }() 

    } 


    //////////////////////////////// 
    // custom cell class below // 
    //////////////////////////////// 

    private class SettingsCustomCell: UITableViewCell { 

     override init(style: UITableViewCellStyle, reuseIdentifier: String?) { 
      super.init(style: style, reuseIdentifier: reuseIdentifier) 

      contentView.addSubview(customCellLabel) 
      customCellLabel.frame = CGRect(x:16, 
              y:0, 
              width: self.frame.width, 
              height:self.frame.height) 
     } 

     required init?(coder aDecoder: NSCoder) { 
      fatalError("init(coder:) has not been implemented") 
     } 

     private let customCellLabel: UILabel = { 

      let aLabel = UILabel() 
       aLabel.text = "custom label" 
       aLabel.textColor = appGreenColor(alphaIs: 1) 

      return aLabel 
     }() 
    } 


    ////////////////////////////////// 
    // custom header class below // 
    ////////////////////////////////// 

    private class SettingsCustomHeader: UITableViewHeaderFooterView { 

     override init(reuseIdentifier: String?) { 
      super.init(reuseIdentifier: reuseIdentifier) 

      contentView.addSubview(customHeaderLabel) 
      customHeaderLabel.frame = CGRect(x:0, 
              y:0, 
              width: self.frame.width, 
              height:self.frame.height) 
     } 

     required init?(coder aDecoder: NSCoder) { 
      fatalError("init(coder:) has not been implemented") 
     } 

     private let customHeaderLabel: UILabel = { 

      let aLabel = UILabel() 
      aLabel.text = "custom header" 
      aLabel.textColor = UIColor.white 
      aLabel.backgroundColor = UIColor.red 

      return aLabel 
     }() 

    } 

和其他.swift文件,我有控制器如下所示。

class SettingsVC: UIViewController, UITableViewDelegate, UITableViewDataSource { 

    private let instanceOfSettingsView = SettingsView() 

    override func loadView() { 
     super.loadView() 

     instanceOfSettingsView.settingsCustomTable.delegate = self 
     instanceOfSettingsView.settingsCustomTable.dataSource = self 

     view.addSubview(instanceOfSettingsView.settingsCustomTable) 
     instanceOfSettingsView.settingsCustomTable.frame = CGRect(x: 0, 
                    y: 0, 
                    width: self.view.frame.width, 
                    height: self.view.frame.height) 

    } 

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return 2 
    } 

    func numberOfSections(in tableView: UITableView) -> Int { 
     return 2 
    } 

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     return tableView.dequeueReusableCell(withIdentifier: "customCell", for: indexPath) 
    } 

    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { 
     return tableView.dequeueReusableHeaderFooterView(withIdentifier: "customHeader") 
    } 

正如您所看到的,我對單元格和標題部分使用完全相同的方法。但由於一些奇怪的原因,我得到了下面的輸出(請查看本文末尾的截圖) ..

你能提醒我缺少什麼嗎?我試圖讓我的觀點與我的控制器分開,除了那小部分外,這是成功的。

感謝您的幫助。

enter image description here

+0

'tableView.dequeueReusableHeaderFooterView'肯定會返回null – zombie

+1

@Gamal Elsayed與您的問題無關,但您應該避免使用框架。 –

+0

@TusharSharma,謝謝,您的評論是解決方案的一個步驟。 –

回答

1

添加一行到您的自定義標題的init FUNC:

override init(reuseIdentifier: String?) { 
     super.init(reuseIdentifier: reuseIdentifier) 

     contentView.addSubview(customHeaderLabel) 
     customHeaderLabel.frame = CGRect(x:0, 
             y:0, 
             width: self.frame.width, 
             height:self.frame.height) 

     // add this line 
     print("w:", self.frame.width, "h:", self.frame.height) 
    } 

你會看到,在這一點上,框架沒有設置。您調試控制檯輸出應該是:

w: 0.0 h: 0.0 
w: 0.0 h: 0.0 

如果您在customHeaderLabel設置的限制,而不是設置其框架,你應該看到的標籤。 (它也可以幫助設置背景顏色,以便您可以看到是什麼)。

所以, 「填寫」 這個答案...

使用Visual格式語言:

override init(reuseIdentifier: String?) { 
    super.init(reuseIdentifier: reuseIdentifier) 

    contentView.addSubview(customHeaderLabel) 

    customHeaderLabel.translatesAutoresizingMaskIntoConstraints = false 

    NSLayoutConstraint.activate(NSLayoutConstraint.constraints(withVisualFormat: "H:|-8-[chl]|", options: [], metrics:[:], views:["chl":customHeaderLabel])) 
    NSLayoutConstraint.activate(NSLayoutConstraint.constraints(withVisualFormat: "V:|[chl]|", options: [], metrics:[:], views:["chl":customHeaderLabel])) 

} 

或者用錨和.constraint格式:

override init(reuseIdentifier: String?) { 
    super.init(reuseIdentifier: reuseIdentifier) 

    contentView.addSubview(customHeaderLabel) 

    customHeaderLabel.translatesAutoresizingMaskIntoConstraints = false 

    customHeaderLabel.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 8.0).isActive = true 
    customHeaderLabel.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 0.0).isActive = true 
    customHeaderLabel.rightAnchor.constraint(equalTo: contentView.rightAnchor, constant: 0.0).isActive = true 
    customHeaderLabel.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: 0.0).isActive = true 

} 
+0

謝謝。在** SettingsCustomHeader **類中添加以下內容已經解決了這個問題.. _NSLayoutConstraint.activate(withVisualFormat:「H:| -8- [chl] |」,options:[],metrics:[:] ,views:[「chl」:customHeaderLabel]))_然後在另一行 _NSLayoutConstraint.activate(NSLayoutConstraint.constraints(withVisualFormat:「V:| [chl] |」,options:[],metrics:[:], views:[「chl」:customHeaderLabel]))_ –

+1

很高興你能解決這個問題......我用你的筆記更新了我的答案(爲了這篇文章的其他人的利益)。 – DonMag