[英]Swift - Programmatically set UITableViewCell resets on scroll
因此,当我选择UITableViewCell的布局时,将以编程方式对其进行设置:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
self.selectedCellIndexPath = indexPath
var selectedCell = tableView.cellForRowAtIndexPath(indexPath)!
tableView.beginUpdates()
tableView.endUpdates()
var cell:SelectedPatientCell = tableView.dequeueReusableCellWithIdentifier("patient selected", forIndexPath: indexPath) as! SelectedPatientCell
cell.patientName.text = patients[indexPath.row].fullName
cell.dob.text = patients[indexPath.row].dob
...
selectedCell = cell
}
当我滚动tableView时,单元格的布局将重置为在cellForRowAtIndexPath中设置的原始布局。 但是,当我在上面的函数中设置高度时,高度保持应有的状态。 有谁知道如何解决这一问题?
这是发生了什么的相册: http : //imgur.com/a/OUIMJ
图片1:原始状态
图2:选定状态(如何保持滚动状态)
图片3:实际发生的情况
你应该保持这种状态
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if indexPath == self.selectedCellIndexPath {
var cell:SelectedPatientCell = tableView.dequeueReusableCellWithIdentifier("patient selected", forIndexPath: indexPath) as! SelectedPatientCell
cell.patientName.text = patients[indexPath.row].fullName
cell.dob.text = patients[indexPath.row].dob
return cell
}
let cell = tableView.dequeueReusableCellWithIdentifier("patient selected") as! OriCell
...
return cell
}
这样,如果您滚动tableView,它将不会恢复到原始Cell。 希望很清楚。
所以我找到了自己的解决方案:
tableView.beginUpdates()
tableView.endUpdates()
我需要在函数结尾处执行此操作:
tableView.reloadData()
这解决了问题
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.