我有一個tableview,它使用UITableViewAutomaticDimension來計算單元格高度。當我點擊一個Twitter時,我試圖讓這些單元格擴展,但即使單元格恢復到正常大小,我仍然遇到了單元格擴展的問題。我希望每個單元格都以一個常量值(例如50)展開,但我似乎無法抓住選定的單元格高度並將其添加50,因爲在cellForRowAtIndexPath
之前調用了heightForRowAtIndexPath
。任何人都可以瞭解我能做什麼?如果我使用UITableViewAutomaticDimension,如何增加單元格的高度?
這裏是我的代碼:
var selectedIndexPath: NSIndexPath? = nil
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if !hasImageAtIndexPath(indexPath) {
var cell = tableView.cellForRowAtIndexPath(indexPath) as! TimelineTableViewCell
} else {
var cell = tableView.cellForRowAtIndexPath(indexPath) as! TimelineTableViewCellImage
}
switch selectedIndexPath {
case nil:
selectedIndexPath = indexPath
default:
if selectedIndexPath! == indexPath {
selectedIndexPath = nil
} else {
selectedIndexPath = indexPath
}
}
tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
}
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
let smallHeight: CGFloat = UITableViewAutomaticDimension
//Here is where I'm having trouble. Cells have a height of 50 and not current height +50
let expandedHeight: CGFloat = UITableViewAutomaticDimension+50
let ip = indexPath
if selectedIndexPath != nil {
if ip == selectedIndexPath! {
return expandedHeight
} else {
return smallHeight
}
} else {
return smallHeight
}
}