Ios 单击时如何更改单个单元格的高度?
单击时,我必须调整tableView的一行大小。我怎么能做到?有人能帮我吗 我的视图控制器类:Ios 单击时如何更改单个单元格的高度?,ios,uitableview,swift,ios8,xcode6,Ios,Uitableview,Swift,Ios8,Xcode6,单击时,我必须调整tableView的一行大小。我怎么能做到?有人能帮我吗 我的视图控制器类: class DayViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { @IBOutlet var daysWorkPointTable: UITableView override func viewDidLoad() { super.viewDidLoad()
class DayViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet var daysWorkPointTable: UITableView
override func viewDidLoad() {
super.viewDidLoad()
var nipName = UINib(nibName: "daysWorkPointsCell", bundle: nil)
self.daysWorkPointTable.registerNib(nipName, forCellReuseIdentifier: "daysWorkCell")
}
func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(tableView:UITableView!, heightForRowAtIndexPath indexPath:NSIndexPath) -> CGFloat {
return 75
}
func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
var cell = tableView.dequeueReusableCellWithIdentifier("daysWorkCell", forIndexPath: indexPath) as daysWorkPointsCell
return cell
}
func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
}
}
首先,您必须跟踪属性中当前选定单元格的indexPath:
var selectedCellIndexPath: NSIndexPath?
它应该是可选的,因为您不能选择任何单元格。
接下来,让我们为选定和未选定状态声明高度(将值更改为您想要的值):
现在您必须实现tableView(u:,heightforrowatinexpath:)
:
现在,在您的表视图(:,didSelectRowAtIndexPath:)
方法中,您必须检查所选行或未选行是否已被点击:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if selectedCellIndexPath != nil && selectedCellIndexPath == indexPath {
selectedCellIndexPath = nil
} else {
selectedCellIndexPath = indexPath
}
tableView.beginUpdates()
tableView.endUpdates()
if selectedCellIndexPath != nil {
// This ensures, that the cell is fully visible once expanded
tableView.scrollToRowAtIndexPath(indexPath, atScrollPosition: .None, animated: true)
}
}
beginUpdates()
和endUpdates()
调用将为您提供动态的高度更改
如果要更改高度更改动画的持续时间,可以将BeginUpdate()
和EndUpdate()
调用包装在动画块UIView.animationWithDuration(…)
中,并将其设置为所需的任何值
您可以查看哪些代码演示了此代码的实际操作。Argh,只需阅读您希望此代码仅在单个单元格上工作即可。我的实现有点过头了,因为它适用于任意数量的单元格。我无法在验证中为selectedCellIndexPath赋值,因为它声明为let,所以我尝试将let更改为var,但继续执行错误,因为我无法为NSIndexPath赋值为nil。你能帮助我吗?我做错了什么?我将赋值改为显式赋值给self.selectedCellIndexath。希望它现在能起作用;)。使用BeginUpdate()和EndUpdate后,ScrollToRowatineXpath工作正常。但是调用这两个方法后,tableView.setContentOffset(z,animated:)不起作用!你知道为什么吗?看起来像是一个bug(((截至2020年9月,BeginUpdate/EndUpdate不仅为您提供动画,而且是它工作所必需的。)
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
if selectedCellIndexPath == indexPath {
return selectedCellHeight
}
return unselectedCellHeight
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if selectedCellIndexPath != nil && selectedCellIndexPath == indexPath {
selectedCellIndexPath = nil
} else {
selectedCellIndexPath = indexPath
}
tableView.beginUpdates()
tableView.endUpdates()
if selectedCellIndexPath != nil {
// This ensures, that the cell is fully visible once expanded
tableView.scrollToRowAtIndexPath(indexPath, atScrollPosition: .None, animated: true)
}
}