Muh*_*med 1 uitableview ios swift
我有表视图来显示用户的评论
我需要根据内容高度使每行的高度动态化
我搜索了它,我发现了
heightForRowAtIndexPath方法
但它不起作用或我不知道如何使用它!
这是我的代码:
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
let username = cell.viewWithTag(1) as! UILabel
let comment = cell.viewWithTag(2) as! UITextView
username.text = usernames[indexPath.row]
comment.text = comments[indexPath.row]
return cell
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.comments.count
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return UITableViewAutomaticDimension;
}
Run Code Online (Sandbox Code Playgroud)
您尚未heightForRowAtIndexPath
正确实施该方法.您应该阅读自我调整表格视图单元格,因为它将执行您想要的操作.
基本上,要使用自调整单元格,UITableView
您将设置估计的行高,并将rowHeight设置为值UITableViewAutomaticDimension
.
tableView.estimatedRowHeight = 85.0
tableview.rowHeight = UITableViewAutomaticDimension`
Run Code Online (Sandbox Code Playgroud)
将估计的行高度值设置为非常接近所有单元格的粗略平均高度的值.这有助于iOS了解完整UIScrollView
内容的大小.
此外,对于自定尺寸单元,您根本不会实现heightForRowAtIndexPath
.每个单元高度从每个单元内的约束中获得.
有关自我调整单元格的详细指南,请查看本教程.
如果您不想自定义单元格,可以实现heightForRowAtIndexPath
,但需要为每个单元格返回正确的高度.该逻辑将取决于您根据indexPath
参数确定.但是您需要确保返回每个单元格(指定的indexPath
)的高度(以像素为单位)(逻辑像素).
归档时间: |
|
查看次数: |
6590 次 |
最近记录: |