2017-03-23 31 views
1

我正在使用Swift 3.我有可擴展的表格視圖單元格,但是有可能根據單擊哪個單元格來獲取不同的行高度?例如,如果第一個單元格被點擊,我希望它返回420高度,如果其他單元格被點擊,我希望它返回300高度。Swift 3 - 有條件的擴展表格視圖單元格高度

這是我的細胞類。

class ResultsCell: UITableViewCell { 

    @IBOutlet weak var introPara : UITextView! 
    @IBOutlet weak var section_heading : UILabel! 

    class var expandedHeight : CGFloat = { get { return 420.0 } } 
    class var defaultHeight : CGFloat { get { return 44.0 } } 

    var frameAdded = false 

    required init?(coder aDecoder: NSCoder) { 
     super.init(coder: aDecoder) 
    } 

    override func awakeFromNib() { 
     super.awakeFromNib() 
     section_heading.translatesAutoresizingMaskIntoConstraints = false 

    } 

    func checkHeight() { 
     introPara.isHidden = (frame.size.height < ResultsCell.expandedHeight) 
    } 

    func watchFrameChanges() { 
     if(!frameAdded) { 
      addObserver(self, forKeyPath: "frame", options: .new, context: nil) 
      checkHeight() 
     } 
    } 

    func ignoreFrameChanges() { 
     if(frameAdded){ 
      removeObserver(self, forKeyPath: "frame") 
     } 
    } 

    deinit { 
     print("deinit called"); 
     ignoreFrameChanges() 
    } 

    // when our frame changes, check if the frame height is appropriate and make it smaller or bigger depending 
    override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) { 
     if keyPath == "frame" { 
      checkHeight() 
     } 
    } 

} 

我試過的是這樣的。

var _expandedHeight : CGFloat = 420.0 
class var expandedHeight : CGFloat { get { return _expandedHeight } set (newHeight) { _expandedHeight = newHeight } } 
var isRow0 = false 

override func awakeFromNib() { 
    super.awakeFromNib() 
    section_heading.translatesAutoresizingMaskIntoConstraints = false 

    if isRow0 { 
     ResultsCell.expandedHeight = 300.0 
    } 
    else { 
     ResultsCell.expandedHeight = 420.0 
    } 
} 

然後在上的getter/setter線TableViewController ...

// return the actual view for the cell 
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let resultcell = tableView.dequeueReusableCell(withIdentifier: "resultCellTemplate", for: indexPath) as! ResultsCell 

    if indexPath.row == 0 { 
     resultcell.isRow0 = true 
    } else { 
     resultcell.isRow0 = false 
    } 

    return resultcell 
} 

但我發現了錯誤:instance member _expandedHeight cannot be used on type ResultsCell

我如何能實現我想要的行爲?

回答

0

使用的tableview更新方法重新評估每個單元的高度

self.tableView.beginUpdates() 
self.tableView.endUpdates() 

更改到的tableview細胞的高度將自動與heightForRowAtIndexPath 計算嘗試設置你的身高WRT indexpath 化妝用戶你提到的heightForRowAtIndexPath計算正確的高度。

0

您應該在TableView的delegate中執行此操作。

您的TableView有一個delegate私有實例變量,其值應該實現協議UITableViewDelegate。該協議包含許多功能,UITableView爲了設置自己的外觀而調用。

欲瞭解更多信息:

UITableViewDelegate協議: https://developer.apple.com/reference/uikit/uitableviewdelegate

UITableViewdelegate實例變量:你想要做什麼 https://developer.apple.com/reference/uikit/uitableview#delegate

是實現你UITableViewDelegate對象的tableView(_:heightForRowAt:)方法。此方法有兩個參數:第一個是UITableView本身,第二個是準備渲染的行的IndexPathIndexPath是一個對象,它具有與正在呈現的單元格對應的sectionrow屬性。

你想要做的是根據它的IndexPath計算一個單元格是否擴展。

斯威夫特IndexPath參考:https://developer.apple.com/reference/foundation/indexpath

嘗試基礎上,UITableViewCell自己做,這是一個不正確的做法,因爲通常它呈現前不應該分析細胞。

一種方法是讓您的UITableView -inheriting類保留一個布爾值數組(可能是嵌套的),以指示哪些單元格應該展開或不展開。然後,您可以使用IndexPathrow和可能的section作爲此數組中的索引,以確定單元格是否展開。事情是這樣的:

class MyTableView: UITableView, UITableViewDelegate { 
    /* ... */ 

    private var expanded: [Bool] = [] 

    init() { 
    super.init() 
    self.delegate = self 
    } 

    func setData(data: [SomeType]) { 
    //set the data for the table here 
    expanded = [Bool](repeating: false, count: data.count) 
    } 

    /* ...logic to expand and collapse cells... */ 

    func tableView(_ view: UITableView, 
       heightForRowAt indexPath: IndexPath) { 
    if expanded[indexPath.row] { 
     return expandedHeight 
    } else { 
     return collapsedHeight 
    } 
    } 

    /* ... */ 
} 

正確的方法來設置你的數據將調用一個方法對你UITableViewDataSource,但我假設你會設置在自身的TableView數據簡單起見。手動設置UITableView的屬性。 UITableView的基於delegatedataSource的工作流被設置,以便您的表視圖能夠高效工作,並且只在需要時自行更新。

相關問題