【发布时间】:2017-09-17 21:48:08
【问题描述】:
我现在正在开发一个基本的列表锻炼应用程序,它会跟踪锻炼情况,然后是每次锻炼的锻炼情况。我想扩展 TableViewController 的当前“编辑”模式以允许更高级的编辑选项。这是我目前所拥有的:
如您所见,我在表格视图的顶部插入了一个部分,以便可以编辑锻炼的标题。我面临的问题是双重的:
- 点击编辑按钮时没有动画。
- 当您尝试在其中一项练习(深蹲或卧推)上向右滑动时,包含练习的整个部分都会消失。
我首先触发 setEditing 函数上的两个不同函数之一,根据布尔编辑返回 true 或 false 切换到读取模式或编辑模式。
override func setEditing(_ editing: Bool, animated: Bool) {
super.setEditing(editing, animated: true)
tableView.setEditing(editing, animated: true)
if editing {
switchToEditMode()
} else {
switchToReadMode()
}
}
然后我将“addTitle”部分(第二张图片中的文本字段)插入一个名为 tableSectionsKey 的数组中,我用它来确定如何显示表格(见下文),然后重新加载表格数据。
func switchToEditMode(){
tableSectionsKey.insert("addTitle", at:0)
self.tableView.reloadData()
}
func switchToReadMode(){
tableSectionsKey.remove(at: 0)
self.tableView.reloadData()
}
这是我的 tableView 数据方法。基本上它的要点是我有一个名为 tableSectionsKey 的数组,我上面提到了,我根据我所处的模式和应该显示的信息添加与部分相关的字符串。最初它只有“addExercise”,它与“将锻炼添加到常规”单元格相关
class WorkoutRoutineTableViewController: UITableViewController, UITextFieldDelegate, UINavigationControllerDelegate {
var tableSectionsKey = ["addExercise"]
}
然后在 viewDidLoad 中,如果当前的锻炼例程有,我添加“锻炼”部分(用于锻炼列表),如果它处于新模式,我添加 addTitle 部分,用于确定视图控制器是否正在从添加新锻炼按钮或从现有锻炼列表中访问(以确定该页面是用于创建锻炼还是更新现有锻炼)
override func viewDidLoad() {
super.viewDidLoad()
if workoutRoutine.exercises.count > 0 {
tableSectionsKey.insert("exercise", at:0)
}
if mode == "new" {
tableSectionsKey.insert("addTitle", at: 0)
}
}
然后在 cellForRowAt 函数中,我根据表格部分与 tableSectionsKey 数组中的索引的关系来确定如何设置单元格样式
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let section = indexPath.section
let sectionKey = tableSectionsKey[section]
let cellIdentifier = sectionKey + "TableViewCell"
switch sectionKey {
case "addTitle":
guard let addTitleCell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as? AddTitleTableViewCell else {
fatalError("Was expecting cell of type AddTitleTableViewCell.")
}
setUpAddTitleTableViewCell(for: addTitleCell)
return addTitleCell
case "exercise":
guard let exerciseCell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as? ExerciseTableViewCell else {
fatalError("Was expecting cell of type ExerciseTableViewCell.")
}
let exercise = workoutRoutine.exercises[indexPath.row]
setUpExerciseTableViewCell(for: exerciseCell, with: exercise)
return exerciseCell
case "addExercise":
let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath)
return cell
default:
fatalError("Couldn't find section: \(section), in WorkoutRoutineTableView" )
}
}
private func setUpExerciseTableViewCell(for cell: ExerciseTableViewCell, with exercise: Exercise) {
let titleText = exercise.name
let detailsText = "\(exercise.sets)x\(exercise.reps) - \(exercise.weight)lbs"
cell.titleLabel.text = titleText
cell.detailsLabel.text = detailsText
}
private func setUpAddTitleTableViewCell(for cell: AddTitleTableViewCell) {
cell.titleTextField.delegate = self
if (workoutRoutine.title != nil) {
cell.titleTextField.text = workoutRoutine.title
}
// Set the WorkoutRoutineTableViewController property 'titleTextField' to the 'titleTextField' found in the addTitleTableViewCell
self.titleTextField = cell.titleTextField
}
这不是我的全部代码,但我相信它是可能与此问题相关的所有代码。
【问题讨论】:
-
您在问两个完全不相关的问题,并且您没有提供与第二期相关的任何信息。因此,我建议您将这个问题缩小到与进入和退出编辑模式时动画丢失相关的第一个问题。然后,如果需要,发布另一个特定于滑动其中一个练习的问题的问题。请务必清楚地说明您何时以及何时看到并包含与该问题相关的内容。
标签: ios swift uitableview