我有一堆静态单元格的tableview。其中两个单元格中有UIDatePicker
个对象。这两个单元通常不可见(它们的高度为0),只有当用户点击特定单元格时才会显示。我调用reloadRows(at:)
来为单元格高度扩展设置动画(以及当单元格必须再次消失时的收缩)。
我遇到的问题是此函数导致DatePicker显示不正确。当单元格本身扩展得很好时,DatePicker会显示1秒钟,然后由于未知原因淡出。如果我使用tableview.reloadData()
代替reloadRows(at:)
,则DatePicker会显示但我无法获得平滑的展开和缩小动画。我尝试在扩展单元格上设置setNeedsLayout
和setNeedsDisplay
,但都没有帮助。
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if indexPath.section == 1, indexPath.row == 2 {
if showStartDatePicker {
startDatePicker.isHidden = false
endDatePicker.isHidden = true
return 260
} else {
startDatePicker.isHidden = true
return 0
}
}else if indexPath.section == 1, indexPath.row == 4 {
if showEndDatePicker {
endDatePicker.isHidden = false
startDatePicker.isHidden = true
return 260
} else {
endDatePicker.isHidden = true
return 0
}
}else if indexPath.section == 2, indexPath.row == 0 {
// Size of description cell
return 100
}
return 44
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if indexPath.section == 1, indexPath.row == 1 {
//Start date cell
tableView.deselectRow(at: indexPath, animated: true)
showStartDatePicker = !showStartDatePicker
showEndDatePicker = false
}else if indexPath.section == 1, indexPath.row == 3 {
//End date cell
tableView.deselectRow(at: indexPath, animated: true)
showEndDatePicker = !showEndDatePicker
showStartDatePicker = false
}
var indexPathArray: Array<IndexPath> = []
if let startDateTimePickerCellIndexpath = tableView.indexPath(for: startDateTimePickerCell) {
indexPathArray.append(startDateTimePickerCellIndexpath)
}
if let endDateTimePickerCellIndexpath = tableView.indexPath(for: endDateTimePickerCell) {
indexPathArray.append(endDateTimePickerCellIndexpath)
}
self.tableView.beginUpdates()
self.tableView.reloadRows(at: indexPathArray, with: .automatic)
self.tableView.endUpdates()
}