在浏览完所有其他Stack Overflow表单之后,我已经为我的一个单元格实现了动态高度,如下所示:
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,其他一切都能正常工作。你们中有没有人看到这个函数没有被调用的原因?
谢谢你的帮助!
发布于 2017-02-02 23:55:33
在Swift 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
}发布于 2018-05-30 12:56:26
对我来说,我必须这么做。
self.tableviewObj.delegate = self所以,不要忘记将委托添加到self中。因为heightForRowAtIndexparth是在委托协议中声明的。
发布于 2019-04-30 15:58:39
在我的例子中,我有三个控制器A,B,C。A是基本控制器,B从A继承,C从B继承。我在A中设置了tableView委托,在B中实现了heightForRowAt函数,当我使用C时出现了问题。我猜是因为A中分配的代理最终指向C,所以B中的方法没有被调用。
https://stackoverflow.com/questions/42006650
复制相似问题