【发布时间】:2016-03-26 08:32:40
【问题描述】:
我在 UIView 中有一个 UITableView,我在它上面执行函数 insertRowsAtIndexPaths。出现更多内容,但 UITableView 高度保持不变。几天来,我已经阅读了数百篇 SO 帖子,似乎无法理解为什么不更新。
var tableView = UITableView()
tableView.frame = CGRectMake(0, 0, self.view.frame.width, view.frame.height)
tableView.estimatedRowHeight = 40
tableView.scrollEnabled = true
tableView.userInteractionEnabled = true
tableView.rowHeight = UITableViewAutomaticDimension
tableView.delegate = self
tableView.dataSource = self
然后如果我点击一个单元格..
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let (parent, isParentCell, actualPosition) = self.findParent(indexPath.row)
self.tableView.beginUpdates()
self.updateCells(parent, index: indexPath.row)
self.tableView.endUpdates()
var frame : CGRect = self.tableView.frame
print(frame.size) // Returns -> (375.0, 667.0)
frame.size = self.tableView.contentSize
print(frame.size) // Return -> (375.0, 1151.0)
self.tableView.frame = frame
}
但 UITableView 的高度仍然为 667.0,如下所示:
.. 即使您现在可以清楚地看到 contentSize 超出了界限。
我可能会在这里遗漏什么?
更新
-- 这是我如何绘制cellForRowAtIndexPath ..
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell : UITableViewCell!
let (parent, isParentCell, actualPosition) = self.findParent(indexPath.row)
if !isParentCell {
cell = tableView.dequeueReusableCellWithIdentifier(childCellIdentifier, forIndexPath: indexPath)
cell.textLabel!.text = self.dataSource[parent].childs[indexPath.row - actualPosition - 1]
cell.textLabel!.textColor = UIColor(red: 35/255.0, green: 31/255.0, blue: 32/255.0, alpha: 1.0)
cell.textLabel!.font = UIFont(name: "STHeitiTC-Light", size: 16)
cell.textLabel!.layer.borderColor = UIColor.purpleColor().CGColor
cell.textLabel!.layer.borderWidth = 1.0
}
else {
cell = tableView.dequeueReusableCellWithIdentifier(parentCellIdentifier, forIndexPath: indexPath)
cell.textLabel!.text = self.dataSource[parent].title
cell.textLabel!.textColor = UIColor(red: 66/255.0, green: 116/255.0, blue: 185/255.0, alpha: 1.0)
cell.textLabel!.font = UIFont(name: "STHeitiTC-Light", size: 20)
}
cell.textLabel!.frame = CGRectMake(0,0,self.view.frame.width, CGFloat.max)
cell.selectionStyle = .None
cell.textLabel!.translatesAutoresizingMaskIntoConstraints = false
cell.textLabel!.numberOfLines = 0
cell.textLabel!.sizeToFit()
cell.textLabel!
return cell
}
问题的可视化..
【问题讨论】:
-
向tableview添加新行时contentSize会增加,tableview的frame会和tableview在content size增加后滚动
-
@Pyro 啊有趣。听起来好像有其他东西阻止了我的 tableView 可滚动。我还能在我的代码中向您展示您认为会阻止我向下滚动以查看新内容的其他内容吗?
标签: ios swift uitableview constraints