2017-05-11 36 views
0

我有一個UITableViewCell的問題,它從xib加載並放置到表中。我用一個自定義高度和我的東西設計了一個xib單元。我有我自己的表:Swift 3 - 從xib加載的UITableViewCell - 自動佈局和高度不正確

class MyTableView: UITableView, UITableViewDataSource, UITableViewDelegate{ 

    let sections: [String] = ["Section 1", "Section 2", "Section 3"] 
    let s1Data: [String] = ["Row 1", "Row 2", "Row 3"] 
    let s2Data: [String] = ["Row 1", "Row 2", "Row 3"] 
    let s3Data: [String] = ["Row 1", "Row 2", "Row 3"] 

    var sectionData: [Int: [String]] = [:] 

    override func awakeFromNib() { 
     super.awakeFromNib() 

     delegate = self 
     dataSource = self 

     register(UINib(nibName: "MyCell", bundle: nil), forCellReuseIdentifier: "MyCell") 

     register(UINib(nibName: "MySection", bundle: nil), forHeaderFooterViewReuseIdentifier: "MySection") 

     sectionData = [0:s1Data, 1:s2Data, 2:s3Data] 

    } 

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) 
     -> Int { 
      return (sectionData[section]?.count)! 
    } 

    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) 
     -> String? { 
      return sections[section] 
    } 

    func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
     return sections.count 
    } 

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

     let section = self.dequeueReusableHeaderFooterView(withIdentifier: "MySection") 

     return section 

    } 

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

     var cell = tableView.dequeueReusableCell(withIdentifier: "MyCell") 

     return cell! 

    } 

} 

正如你所看到的一切是非常基本的,我不清理了,但(只是展示)。該部分工作正常,並顯示正確。但MyCell完全錯誤。沒有高度應用於行本身(所以內容或多或少被切割),我在xib中設置的自動佈局不存在。這只是一團糟。

在xib中,我設置行的高度也是這樣。 RowHeight

我也試過以下兩行:

rowHeight = UITableViewAutomaticDimension 
    estimatedRowHeight = 96.0 

在XIB我設置正確的限制,以迫使細胞擴大。

tableview本身只是放置在ViewController中,並配置爲MyTableView類。

那麼爲什麼高度和自動佈局不適用於單元?有人可以在這裏給我一個建議。

編輯:

這是電池的XIB文件:

xib

這是表的結果:

table

,你可以看到自動佈局的東西現在起作用。也許在我身邊有一個錯誤,但身高仍然不正確

+0

如果你想靜態的高度,而不是讓自動佈局算一下,設置'的rowHeight = 96' – redent84

回答

0

這聽起來像你有Xib內的實際單元格,你把約束等等。如果是這樣的話,那麼你應該改變它只是使用標籤或您需要的東西。

無論哪種方式,我會建議通過創建一個它自己的類的Xib組件。

class DesignCell: UITableViewCell { 

    @IBOutlet weak var headerLabel: UILabel! 

    public func configure(withText text: String) { 
      headerLabel.text = text 
    } 
} 

現在你可以使用你的電池數據源的tableView

let cell = tableView.dequeueReusableCell(withIdentifier: DesignCell.identifier) as! DesignCell 
    cell.configure(withText: section[indexPath.item]) 
    return cell 
0

內刪除您:

rowHeight = UITableViewAutomaticDimension

estimatedRowHeight = 96.0

將這個方法本身根據您的需求,您的表的每一行牛逼高度:

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 

    return 96 

} 

或者

只需self.tableView.rowHeight = 96只是爲所有的細胞靜態高度。

+0

爲什麼就不能'的rowHeight = 96'? – redent84

+1

但是,如果OP想要根據各種因素改變'rowHeight',那麼做就行,否則簡單的'rowHeight = 96'就足夠了。 –

+0

我同意委託方法更靈活,但對於靜態高度'rowHeight'更簡單 – redent84

0

我有一個解決方案。我只是在背景中設置一個新的視圖,或者在標籤本身設置一個與單元高度相等的高度約束。所以在我的情況下,96

set height

+0

有點矯枉過正,爲什麼使用自動佈局來計算高度,然後強制它的常量? – redent84

相關問題