swift UITableView 设置 rowHeight [英] swift UITableView set rowHeight
本文介绍了swift UITableView 设置 rowHeight的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用以下代码将 tableView
中每一行的高度设置为相应单元格的高度:
I am trying to set the height of each row in the tableView
to the height of the corresponding cell with this code:
override func tableView(tableView: UITableView!, heightForRowAtIndexPath indexPath: NSIndexPath!) -> CGFloat {
var cell = tableView.cellForRowAtIndexPath(indexPath)
return cell.frame.height
}
初始化 var cell
时出现此错误:
I get this error when initialising var cell
:
线程 1:EXC_BAD_ACCESS(code=2,address=0x306d2c)
Thread 1:EXC_BAD_ACCESS(code=2,address=0x306d2c)
推荐答案
设置行高有单独的方法:
For setting row height there is separate method:
对于 Swift 3
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 100.0;//Choose your custom row height
}
较旧的 Swift 使用
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 100.0;//Choose your custom row height
}
否则您可以使用以下方法设置行高:
self.tableView.rowHeight = 44.0
在ViewDidLoad中.
这篇关于swift UITableView 设置 rowHeight的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文