通過所有的其他堆棧溢出形式去後,我實現了一個動態的高度,我的細胞之一,如下所示:的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和其他一切正常工作。你們有沒有看到有沒有這個功能被調用的原因?
謝謝你的幫助!
如下所述,UITableViewAutomaticDimension不是應該用於算術的值。返回UITableViewAutomaticDimension * 3將產生不可預知的影響。 – PeejWeej
請注意,調用'heightForRowAt'可能很昂貴;你也可以調用'self.tableView.rowHeight = UITableViewAutomaticDimension'和 'self.tableView.estimatedRowHeight = 72.0 //估計,'viewDidLoad'中的平均值。也例如在這裏:https://www.appcoda.com/self-sizing-cells/ – Koen