简体   繁体   English

在“编辑”模式下将部分添加到UITableView

[英]Adding sections to UITableView in 'Edit' Mode

I'm working on a basic list workout app right now that keeps track of workouts and then the exercises for each workout. 我现在正在使用基本列表锻炼应用程序,该应用程序跟踪锻炼情况,然后跟踪每次锻炼的锻炼情况。 I want to extend the current 'editing' mode of a TableViewController to allow for more advanced editing options. 我想扩展TableViewController的当前“编辑”模式,以允许使用更多高级编辑选项。 Here is what I have so far: 这是我到目前为止的内容:

在此处输入图片说明 在此处输入图片说明

As you can see, I am inserting a section at the top of the table view so that the title of the workout can be edited. 如您所见,我将在表格视图的顶部插入一个部分,以便可以编辑锻炼的标题。 The problem I am facing is twofold: 我面临的问题是双重的:

  1. There is no animation when the edit button is tapped anymore. 再次点击编辑按钮时,没有动画。
  2. When you try to swipe right on one of the exercises (Squat or Bench press) the entire section containing exercises disappears. 当您尝试在某个练习上向右轻扫(深蹲或卧推)时,包含练习的整个部分将消失。

I start by triggering one of two different functions on the setEditing function, to either switch to read mode or edit mode based on whether the boolean editing returns true or false. 我首先触发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()
    }
}

Then I either insert the "addTitle" section (the text field seen in the second image) to an array called tableSectionsKey which I use to determine how to display the table (seen further below), and then reload the table data. 然后,我将“ addTitle”部分(在第二个图像中看到的文本字段)插入到名为tableSectionsKey的数组中,该数组用于确定如何显示表(见下文),然后重新加载表数据。

func switchToEditMode(){
    tableSectionsKey.insert("addTitle", at:0)
    self.tableView.reloadData()
}

func switchToReadMode(){
    tableSectionsKey.remove(at: 0)
    self.tableView.reloadData()
}

Here is my tableView data method. 这是我的tableView数据方法。 Basically the gist of it is that I have the array called tableSectionsKey I mentioned above, and I add strings that relate to sections based on what mode I'm in and what information should be displayed. 基本上,要点是我拥有上面提到的名为tableSectionsKey的数组,并根据所处的模式和应显示的信息添加与节相关的字符串。 Initially it just has "addExercise", which related to the "Add exercise to routine" cell 最初它只有“ addExercise”,与“向例行添加锻炼”单元有关

class WorkoutRoutineTableViewController: UITableViewController, UITextFieldDelegate, UINavigationControllerDelegate {
    var tableSectionsKey = ["addExercise"]
}

Then in viewDidLoad I add the "exercise" section (for list of exercises) if the current workout routine has any, and I add the addTitle section if it's in new mode, which is used to determine if the view controller is being accessed from an add new workout button or a from a list of preexisting workouts (so to determine if the page is being used to create a workout or update an existing one) 然后在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)
    }
}

Then in the cellForRowAt function I determine how to style the cell based on how the section of the table relates with an index in the tableSectionsKey array 然后在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
}

This isn't all of my code but I believe it is all of the code that could be relevant to this problem. 这不是我的全部代码,但我相信可能与该问题有关的所有代码。 If it isn't please let me know what is missing and I will update it. 如果不是,请让我知道缺少的内容,我将对其进行更新。

Thanks in advanced to anyone who takes the time to answer me. 在此先感谢所有花时间回答我的人。

Your animation issue is due to the use of reloadData . 您的动画问题是由于使用reloadData You need to replace the uses of reloadData with calls to insert or delete just the one section. 您需要用调用来替换reloadData的用法,以仅插入或删除一个部分。

func switchToEditMode(){
    tableSectionsKey.insert("addTitle", at:0)
    let section = IndexSet(integer: 0)
    self.tableView.insertSections(section, with: .left) // use whatever animation you want
}

func switchToReadMode(){
    tableSectionsKey.remove(at: 0)
    let section = IndexSet(integer: 0)
    self.tableView.deleteSections(section, with: .left) // use whatever animation you want
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM