【发布时间】:2016-01-25 16:04:00
【问题描述】:
我有一个包含几个堆栈视图的单元格,底部堆栈视图包含一个 textView 和一个自定义分隔符。
我想创建一个选项,当用户点击单元格时,它会显示点击文本视图的整个文本,因此该单元格中的最大行数为 0,而其他单元格中的最大行数应为 3。
我用了这个教程http://www.roostersoftstudios.com/2011/04/14/iphone-uitableview-with-animated-expanding-cells/,稍微修改了一下,我的代码:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(iden_tableViewCell4) as! TableViewCell4
if let selectedCellIP = selectedIndexPath {
if indexPath == selectedCellIP {
cell.textTextVIew.textContainer.maximumNumberOfLines = 0
}
else {
cell.textTextVIew.textContainer.maximumNumberOfLines = textVIewMaxNumberOfLines
}
}
else {
cell.textTextVIew.textContainer.maximumNumberOfLines = textVIewMaxNumberOfLines
}
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated: true)
//The user is selecting the cell which is currently expanded
//we want to minimize it back
if selectedIndexPath == indexPath {
selectedIndexPath = nil
tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
return
}
//First we check if a cell is already expanded.
//If it is we want to minimize make sure it is reloaded to minimize it back
if let selIndexPath = selectedIndexPath {
let previousPath = selIndexPath
selectedIndexPath = indexPath
tableView.reloadRowsAtIndexPaths([previousPath], withRowAnimation: .Fade)
}
//Finally set the selected index to the new selection and reload it to expand
selectedIndexPath = indexPath
tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
}
在我的viewDidLoad 我设置
tableView.estimatedRowHeight = 160
tableView.rowHeight = UITableViewAutomaticDimension
扩展和收缩效果很好,但是其他单元格的 textView 高度有一个奇怪的行为。当第一个单元格被扩展时,我向下滚动,最后一个单元格也被扩展,不应该。
【问题讨论】:
-
你的 heightForRowAtIndexPath 是什么样的?
-
因为
tableView.estimatedRowHeight = 160 tableView.rowHeight = UITableViewAutomaticDimension我没用过 -
在使用
estimatedRowHeight+UITableViewAutomaticDimension时不应该有heightForRowAtIndexPath。 -
从我知道的另一个项目中,如果在 cellForRow 中的 if-else 语句中设置了某些内容,并且在 else 分支中没有取消设置,它也可能有奇怪的行为,但我不这么认为是我的情况,因为我已经覆盖了 cellForRow 中的所有 if else 分支,还是没有?
标签: ios swift uitableview uitextview