2017-02-02 25 views
1

通過所有的其他堆棧溢出形式去後,我實現了一個動態的高度,我的細胞之一,如下所示:的TableView - heightForRowAtIndexPath不會被調用

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    var cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as UITableViewCell 

    if(indexPath.row == 1){ 
     var image : UIImage = maskRoundedImage(image: UIImage(named: "Temp_Profile.png")!, radius: 150) 
     cell.imageView?.image = image 
     return cell 
    } 
    cell.textLabel?.text = TableArray[indexPath.row] 
    cell.backgroundColor = UIColor.init(colorLiteralRed: 0.56, green: 0, blue: 0.035, alpha: 1) 
    cell.textLabel?.textColor = UIColor.white 
    cell.textLabel?.font = UIFont(name: "Helvetica", size: 28) 

    return cell 
} 
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { 
    if indexPath.row == 1 { 
     return UITableViewAutomaticDimension * 3 

    } 
    return UITableViewAutomaticDimension 
} 

似乎很簡單,但調試會話顯示heightForRowAtIndexPath永遠不會被調用。視圖控制器是一個UITableViewController和其他一切正常工作。你們有沒有看到有沒有這個功能被調用的原因?

謝謝你的幫助!

+0

如下所述,UITableViewAutomaticDimension不是應該用於算術的值。返回UITableViewAutomaticDimension * 3將產生不可預知的影響。 – PeejWeej

+0

請注意,調用'heightForRowAt'可能很昂貴;你也可以調用'self.tableView.rowHeight = UITableViewAutomaticDimension'和 'self.tableView.estimatedRowHeight = 72.0 //估計,'viewDidLoad'中的平均值。也例如在這裏:https://www.appcoda.com/self-sizing-cells/ – Koen

回答

8

在斯威夫特3,簽名是:

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

你有舊的簽名,因此無法識別。

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 
    if indexPath.row == 1 { 
     return UITableViewAutomaticDimension * 3 

    } 
    return UITableViewAutomaticDimension 
} 
+2

對於任何人試圖使用UITableViewAutomaticDimension * 3,不要。它返回一個-1作爲tableView的一個信號,使其成爲默認值。使用實際的號碼 – Ryan

+1

當您從其他地方複製示例時,很容易得到這些方法的舊簽名 - 正如我們所做的一樣!在某些情況下,如果Swift足夠讓事情失效 - XCode會產生錯誤 - 但對於這種情況,您只會收到警告。那就是你不應該忽略那些黃色的三角形。自動完成應該給你正確的簽名,然後其餘的功能很可能會正常。 – Russell

相關問題