我当前正在基于称为内容构建器的类中的内容删除和插入行。下面是一个示例。
func stateDidChange(id: String, _ state: Bool) {
switch id {
case ConfigureExerciseContentBuilderKeys.accessory.rawValue:
exerciseProgramming.isAccessory = state
if state {
if let customIncrementsItemIndex = self.configureExerciseContentBuilder.getContent().firstIndex(where: { ($0 as? FormLabelTextFieldItem)?.id == ConfigureExerciseContentBuilderKeys.increment.rawValue }),
let syncedWorkoutsItemIndex = self.configureExerciseContentBuilder.getContent().firstIndex(where: { ($0 as? ExerciseSyncedWorkouts)?.id == ConfigureExerciseContentBuilderKeys.syncedWorkouts.rawValue }) {
self.configureExerciseContentBuilder = ConfigureExerciseContentBuilder(type: type, exerciseProgramming: exerciseProgramming)
let incrementsIndexPath = IndexPath(item: customIncrementsItemIndex, section: 0)
let syncedIndexPath = IndexPath(item: syncedWorkoutsItemIndex, section: 0)
let spacerIndexPath = IndexPath(item: syncedWorkoutsItemIndex + 1, section: 0)
self.tableView.beginUpdates()
self.tableView.deleteRows(at: [incrementsIndexPath, syncedIndexPath, spacerIndexPath], with: .automatic)
self.tableView.endUpdates()
}
} else {
self.configureExerciseContentBuilder = ConfigureExerciseContentBuilder(type: type, exerciseProgramming: exerciseProgramming)
if let customIncrementsItemIndex = self.configureExerciseContentBuilder.getContent().firstIndex(where: { ($0 as? FormLabelTextFieldItem)?.id == ConfigureExerciseContentBuilderKeys.increment.rawValue }),
let syncedWorkoutsItemIndex = self.configureExerciseContentBuilder.getContent().firstIndex(where: { ($0 as? ExerciseSyncedWorkouts)?.id == ConfigureExerciseContentBuilderKeys.syncedWorkouts.rawValue }) {
let incrementsIndexPath = IndexPath(item: customIncrementsItemIndex, section: 0)
let syncedIndexPath = IndexPath(item: syncedWorkoutsItemIndex, section: 0)
let spacerIndexPath = IndexPath(item: syncedWorkoutsItemIndex + 1, section: 0)
self.tableView.beginUpdates()
self.tableView.insertRows(at: [incrementsIndexPath, syncedIndexPath, spacerIndexPath], with: .automatic) // Insert into the index of where the accessory row was/is
self.tableView.endUpdates()
}
}
default:
break
}
}
根据上面的逻辑,行已成功删除和插入,我使用了自动参数,以便表视图可以最佳方式进行处理。
还值得注意的是,在tableviewcell中,autolayout用于其中的元素,并且我已根据其内容将tableview中的行高配置为动态行。
但是由于某些原因,当我删除或插入随附视频中的绿色框的行时,似乎出现了奇怪的断断续续的动画。有人对为什么会发生这种情况有任何指示/想法吗?