gpt4 book ai didi

swift - 无法根据内容大小动态处理单元格大小

转载 作者:行者123 更新时间:2023-11-30 12:56:23 24 4
gpt4 key购买 nike

我试图根据内容大小动态更改 tableView 单元格高度,但出现错误。

错误-

enter image description here

实际代码-

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
{
let height:CGFloat = self.calculateHeightForString(downloadTableDataa[indexPath.row] as! String)
return height + 70.0
}

func calculateHeightForString(inString:String) -> CGFloat
{
let messageString = inString
let attributes = UIFont.init(name: "Roboto-Regular", size: 15)

let attrString:NSAttributedString? = NSAttributedString(string: messageString, attributes: attributes)
let rect:CGRect = attrString!.boundingRectWithSize(CGSizeMake(300.0,CGFloat.max), options: NSStringDrawingOptions.UsesLineFragmentOrigin, context:nil )//hear u will get nearer height not the exact value
let requredSize:CGRect = rect
return requredSize.height //to include button's in your tableview

}

最佳答案

问题是您尝试将 UIFont 类型的对象传递到属性字段中,而属性字段需要 [String:AnyObject] 类型的字典。这就是错误告诉您的内容。

之前

let attributes = UIFont.init(name:"Roboto-Regular", size:15.0) <---- UIFont
let attrString:NSAttributedString? = NSAttributedString(string: messageString, attributes: attributes) <---- wants a dictionary of type [String:AnyObject]

之后

let stringAttributes = [NSFontAttributeName: UIFont(name: "Roboto-Regular", size: 18.0)!] 
let attrString = NSAttributedString(string: messageString, attributes: stringAttributes)

关于swift - 无法根据内容大小动态处理单元格大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40354419/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com