Changing an Int to a CGFloat in Swift to return heightForRowAtIndexPath tableview function

送分小仙女□ 提交于 2019-12-11 09:39:08

问题


I am sure that I am missing something really simple, but I just can't get it to work.

I will try to explain better what I am doing to try to help others if they have this same issue.

This was my function:

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return (count * 40 + 75) as CGFloat
}

The above function doesn't work. Thanks to the help I received, I found that this is the way to do it:

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return CGFloat(count * 40 + 75)
}

Thanks!


回答1:


Yes, it is simple:

let i = 123 // an Int
let f = CGFloat(i) // a CGFloat

If you command-click on CGFloat then you'll see all its initializers:

init(_ value: Float)
init(_ value: Double)
// ... many more, and finally:
init(_ value: UInt)
init(_ value: Int)


来源:https://stackoverflow.com/questions/27079416/changing-an-int-to-a-cgfloat-in-swift-to-return-heightforrowatindexpath-tablevie

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!