用代码:
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
let message = self.messageList[indexPath.row]
if (message is ResourceMessage) || (message is NotificationMessage) {
return UITableViewAutomaticDimension
}else if message is TextMessage {
let calculatedcellHeight = MessageTableViewCell.calculateCellHeight(message)
// let cell = tableView.cellForRowAtIndexPath(indexPath) as! MessageTableViewCell
// let calculatedcellHeight = cell.calculateCellHeight()
gLog.verbose("[\(indexPath.row)] calculatedcellHeight=\(calculatedcellHeight)")
return calculatedcellHeight
}
//TODO: add other type message support
return UITableViewAutomaticDimension
}
去实现自动布局中让UI自动计算每行cell的高度。
但是,后来去为了实现,滚动的时候,不跳动
而用了:
//insert multiple row while keep table view static not scroll
var tableViewContentOffset = self.messageTableView.contentOffset
gLog.verbose("tableViewContentOffset=\(tableViewContentOffset)")
UIView.setAnimationsEnabled(false)
var indexPaths = [NSIndexPath]()
var totalNewRowHeight:CGFloat = 0
for eachRowIndx in insertedIdxList {
let eachRowIndexPath = NSIndexPath(forRow: eachRowIndx, inSection: 0)
indexPaths.append(eachRowIndexPath)
let eachNewRowHeight:CGFloat = self.tableView(self.messageTableView, heightForRowAtIndexPath: eachRowIndexPath)
totalNewRowHeight += eachNewRowHeight
gLog.verbose("[\(eachRowIndx)] eachNewRowHeight=\(eachNewRowHeight), totalNewRowHeight=\(totalNewRowHeight)")
}
tableViewContentOffset.y += totalNewRowHeight //220.0
// tableViewContentOffset.y -= indicatorContentOffset //200.0
gLog.debug("tableViewContentOffset=\(tableViewContentOffset)") //(0.0, 200.0)
self.messageTableView.beginUpdates()
self.messageTableView.insertRowsAtIndexPaths(indexPaths, withRowAnimation: UITableViewRowAnimation.None)
gLog.info("insert message list \(newMessageList) to indexs \(insertedIdxList)")
self.messageTableView.endUpdates()
UIView.setAnimationsEnabled(true)
self.messageTableView.setContentOffset(tableViewContentOffset, animated: false)
但是对于:
let eachNewRowHeight:CGFloat = self.tableView(self.messageTableView, heightForRowAtIndexPath: eachRowIndexPath)
当返回的是UITableViewAutomaticDimension,值却为-1
2016-04-23 17:36:37.577 [Verbose] [main] [MessageTableViewController.swift:1244] insertMessageList > [13] eachNewRowHeight=88.0, totalNewRowHeight=1022.0
2016-04-23 17:36:59.466 [Verbose] [main] [MessageTableViewController.swift:1244] insertMessageList > [14] eachNewRowHeight=-1.0, totalNewRowHeight=1021.0
tableview heightForRowAtIndexPath UITableViewAutomaticDimension -1
tableview heightForRowAtIndexPath UITableViewAutomaticDimension less than 0
Xcode 7中Static Cells自动计算高度失效的解决方法
tableview UITableViewAutomaticDimension return -1
算了,找不到解决办法。。
自己判断吧
当返回-1的时候,自己去计算高度。。。
转载请注明:在路上 » [未解决]swift的tableView的heightForRowAtIndexPath的UITableViewAutomaticDimension返回-1