我正在制作一个带有文本帖子的应用程序,但我意识到很多文本帖子都会很长,所以我添加了一个“全部显示”按钮,但是当你按下它时,这个单元格会消失,然后向上滚动,然后跳到单元格的中间。因为这很难解释.
http://makeagif.com/kMFG-g
我相信这是因为.
table.rowHeight = UITableViewAutomaticDimension但是,有必要使单元格显示自定义的文本数量。但不管怎样,这是密码。
这是使它发生的实际作用..。
var showingMore = [Bool]()
func showAllAndLess(sender: AnyObject) {
var buttonPosition: CGPoint = sender.convertPoint(CGPointZero, toView: self.table)
var indexPath: NSIndexPath = self.table.indexPathForRowAtPoint(buttonPosition)!
if showingMore[indexPath.row] {
sender.setTitle("Show Less", forState: .Normal)
} else {
sender.setTitle("Show All", forState: .Normal)
}
showingMore[indexPath.row] = !showingMore[indexPath.row]
table.reloadRowsAtIndexPaths([indexPath], withRowAnimation: .None)
}以下是cellForRowAtIndexPath方法中的代码..。
showingMore.append(false)
postCellObj.showAllAndLessButton.hidden = true
if showingMore[indexPath.row] {
postCellObj.message.text = messageString
postCellObj.showAllAndLessButton.setTitle("Show Less", forState: .Normal)
postCellObj.showAllAndLessButton.hidden = false
println("Showing Less")
}
else if count(messageString) >= 800 {
var messageNs = messageString as NSString
var messageFinal = messageNs.substringWithRange(NSRange(location: 0, length: 800))
postCellObj.message.text = messageFinal as String + "..."
postCellObj.showAllAndLessButton.setTitle("Show All", forState: .Normal)
postCellObj.showAllAndLessButton.hidden = false
} else {
postCellObj.message.text = messageString
}
}感谢您的阅读!我希望我能提供足够的信息。如果你需要更多的话就说点什么。(:
变量"messageString“是消息文本。
发布于 2015-09-19 17:28:27
这是一个众所周知的问题。如果修改数据源(添加或删除行),UITableView转储以前计算的单元格高度。这不是问题,当你向下滚动,但一旦你开始滚动表视图开始跳跃。
解决方案是自己缓存单元高度。这里有更多信息:https://github.com/smileyborg/TableViewCellWithAutoLayoutiOS8/issues/17
有时,跳跃式滚动可能是由于错误的细胞高度估计(如果它是一个数量级的话)。在这种情况下,您可以根据文本的数量来调整您的估计值(不需要非常精确)。
这里有一种估计文本高度的方法:
class func estimatedHeightForText(text: String, andWidth width: CGFloat) -> CGFloat {
let paragraphCount = (text.characters.split { $0 == "\n" }.map { String($0) }).count + 1
// 7 is approximate width of character in points
let charsPerLine = width / 7
let lineCount = text.characters.count / Int(round(charsPerLine)) + 1
// 14 is approximate height of line
let estimatedHeight = 14 * CGFloat(max(lineCount, paragraphCount))
return estimatedHeight
}https://stackoverflow.com/questions/32669857
复制相似问题