Bar*_*zyk 3 ios swift uitableviewautomaticdimension
在我的UITableView使用方法滚动到最后一个单元格:
WLNetworkClient.sharedClient().createCommentWIthText(commentTextView.text, forItem: item) { error in
defer {
UIAlertController.showAlertFromError(error)
}
if error == nil {
self.fetchedResultsController.fetchRequest.fetchLimit += 1
try! self.fetchedResultsController.performFetch()
self.tableView.reloadData()
self.scrollTableViewToBottom()
}
}
private func scrollTableViewToBottom() {
tableView.scrollToRowAtIndexPath(NSIndexPath(forRow: fetchedResultsController.fetchedObjects!.count - 1, inSection: 0), atScrollPosition: UITableViewScrollPosition.Bottom, animated: true)
}
Run Code Online (Sandbox Code Playgroud)
但是这没有按预期工作,因为ALWAYS表从顶部滚动,即使我在第二个单元格上也是如此.如何解决这个问题?
dosdos回答在Swift 2中为我工作 添加了一些小更新
宣布伊娃
var heightAtIndexPath = NSMutableDictionary()
Run Code Online (Sandbox Code Playgroud)
在func viewDidLoad()中
func viewDidLoad() {
.... your code
self.tableView.rowHeight = UITableViewAutomaticDimension
}
Run Code Online (Sandbox Code Playgroud)
然后添加以下两种方法:
override func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
let height = self.heightAtIndexPath.objectForKey(indexPath)
if ((height) != nil) {
return CGFloat(height!.floatValue)
} else {
return UITableViewAutomaticDimension
}
}
override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
let height = cell.frame.size.height
self.heightAtIndexPath.setObject(height, forKey: indexPath)
}
Run Code Online (Sandbox Code Playgroud)
SWIFT 3:
var heightAtIndexPath = NSMutableDictionary()
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
let height = self.heightAtIndexPath.object(forKey: indexPath)
if ((height) != nil) {
return CGFloat(height as! CGFloat)
} else {
return UITableViewAutomaticDimension
}
}
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
let height = cell.frame.size.height
self.heightAtIndexPath.setObject(height, forKey: indexPath as NSCopying)
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1391 次 |
| 最近记录: |