Upv*_*ote 6 uitableview ios autolayout swift
目前我正忙着快速和动态的表格单元格高度.我在xcode6.1上为iOS8.1开发了一个简单的应用程序:https: //github.com/ArtworkAD/DynamicCellTest
因此,要实现细胞高度随细胞内容延伸,我会执行以下操作:
self.tableView.rowHeight = UITableViewAutomaticDimensionheightForRowAtIndex方法需要最少的代码:
class MyTableViewController: UITableViewController {
var entries:Array<String> = [String]()
override func viewDidLoad() {
super.viewDidLoad()
//create dummy content
var i = 0
while i < 10 {
entries.append("\(i) Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor")
entries.append("\(i+1) Lorem ipsum dolor sit amet")
i = i + 2;
}
self.tableView.rowHeight = UITableViewAutomaticDimension
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.entries.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCellWithIdentifier("basic_cell", forIndexPath: indexPath) as UITableViewCell
var label = cell.viewWithTag(13)
if let unwrappedLabel = label as? UILabel {
unwrappedLabel.text = self.entries[indexPath.row]
}
return cell
}
}
Run Code Online (Sandbox Code Playgroud)
左图显示了上述代码的结果.细胞高度随着标签的内容而增长,一切都很好.但是,当您将披露指示器单击到详细视图并再次向后移动时,您将获得正确的图像.为什么会这样?

解决此问题的一个不好的解决方案是覆盖此方法:
override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)
self.tableView.reloadData()
}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
self.tableView.reloadData()
}
override func viewDidDisappear(animated: Bool) {
super.viewDidDisappear(animated)
self.tableView.reloadData()
}
Run Code Online (Sandbox Code Playgroud)
这样就解决了上面的问题,但是这个解决方案似乎不对吗?它的一个副作用是,当self.tableView.reloadData()被调用时,表视图端口跳转到第一个看起来不太好的单元格.
有谁知道我做错了什么?随意克隆我的repo https://github.com/ArtworkAD/DynamicCellTest并测试它.
Ani*_*are -2
还在这里遇到了替代方案http://useyourloaf.com/blog/2014/08/07/self-sizing-table-view-cells.html。有评论提到——
只需在从 cellForRowAtIndexPath 返回单元格布局之前将其设置为子视图即可:
[cell setNeedsDisplay];
[cell layoutIfNeeded];
Run Code Online (Sandbox Code Playgroud)

| 归档时间: |
|
| 查看次数: |
5438 次 |
| 最近记录: |