【问题标题】:How to update the row in my AddViewController with the new name如何使用新名称更新我的 AddViewController 中的行
【发布时间】:2020-10-09 23:13:02
【问题描述】:

我想从 AddViewController 更新我的 itemViewController 中的行名称。在我的代码中,我将包含名称和描述的项目对象传递给我的 AddViewController 并更改值,但是行名称永远不会更新。

我浏览了互联网并找到了有关执行此操作的所谓委托模式的信息。我只是不确定如何实现该模式。提前感谢您的帮助。

//  ItemsViewController.swift
//  Project 3-ToDo
//
//  Created by Gracias Claude on 10/9/20.
//

import UIKit

class ItemsViewController : UITableViewController {
    var itemStore: ItemStore!
    
    
    
    @IBAction func addNewItem(_ sender: UIButton) {
        // Create a new item and add it to the store
        let newItem = itemStore.createItem()

        // Figure out where that item is in the array
        if let index = itemStore.allItems.firstIndex(of: newItem) {
            let indexPath = IndexPath(row: index, section: 0)

            // Insert this new row into the table
            tableView.insertRows(at: [indexPath], with: .automatic)
        }
       }

       @IBAction func toggleEditingMode(_ sender: UIButton) {
        // If you are currently in editing mode...
           if isEditing {
               // Change text of button to inform user of state
               sender.setTitle("Edit", for: .normal)

               // Turn off editing mode
               setEditing(false, animated: true)
           } else {
               // Change text of button to inform user of state
               sender.setTitle("Done", for: .normal)

               // Enter editing mode
               setEditing(true, animated: true)
           }
       }
    
    override func tableView(_ tableView: UITableView, 
            numberOfRowsInSection section: Int) -> Int {
        return itemStore.allItems.count
    }
    
    // returning table view cell
    override func tableView(_ tableView: UITableView,
            cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        

            // Get a new or recycled cell
            let cell = tableView.dequeueReusableCell(withIdentifier: "UITableViewCell",
                for: indexPath)

        // Set the text on the cell with the description of the item
        // that is at the nth index of items, where n = row this cell
        // will appear in on the table view
        let item = itemStore.allItems[indexPath.row]

        cell.textLabel?.text = item.name
        cell.detailTextLabel?.text = "\(item.dateCreated)"

        return cell
    }
    
    override func tableView(_ tableView: UITableView,
                            commit editingStyle: UITableViewCell.EditingStyle,
                            forRowAt indexPath: IndexPath) {
        // If the table view is asking to commit a delete command...
        if editingStyle == .delete {
            let item = itemStore.allItems[indexPath.row]

            // Remove the item from the store
            itemStore.removeItem(item)

            // Also remove that row from the table view with an animation
            tableView.deleteRows(at: [indexPath], with: .automatic)
        }
    }
    override func tableView(_ tableView: UITableView,
                            moveRowAt sourceIndexPath: IndexPath,
                            to destinationIndexPath: IndexPath) {
        // Update the model
        itemStore.moveItem(from: sourceIndexPath.row, to: destinationIndexPath.row)
    }
    
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // If the triggered segue is the "showItem" segue
        switch segue.identifier {
        case "showItem":
            print("Hello World")
            // Figure out which row was just tapped
            if let row = tableView.indexPathForSelectedRow?.row {

                // Get the item associated with this row and pass it along
                let item = itemStore.allItems[row]
                let detailViewController
                        = segue.destination as! DetailsViewController
                detailViewController.item = item
            }
            break;
        
        case "addItem":
            let row = tableView.indexPathForSelectedRow?.row ?? 0

//                // Get the item associated with this row and pass it along
                let item = itemStore.allItems[row]
                let addViewController
                        = segue.destination as! AddViewController
                addViewController.item = item
//            }
            break;
        default:
            preconditionFailure("Unexpected segue identifier.")
        }
    }
}

//AddViewController

//
//  AddViewController.swift
//  Project 3-ToDo
//
//  Created by Gracias Claude on 10/9/20.
//

import UIKit

class AddViewController : UIViewController {
    
    
    var item: Item!;
   
  
    @IBOutlet weak var itemNameField: UITextField!
    @IBOutlet weak var priorityField: UITextField!
    @IBOutlet weak var descriptionField: UITextField!
    
    
    @IBAction func submitItem(_ sender: UIButton) {
        //how to update the existing Row name
        item.name = itemNameField.text ?? "Default Text"
        item.description = descriptionField.text ?? "default Description"
        
        return;
      
        
    }
}

【问题讨论】:

    标签: swift swift3 uikit


    【解决方案1】:

    据我了解,您正在将数据从您的ItemViewController 传递到AddViewController。然后,您从 AddViewController 更改该值,并且您希望将该更改反映回 ItemViewController。

    如果是这种情况,您必须将已修改的数据传回ItemViewController,有多种方法可以做到这一点。

    最简单的方法(在我看来不是最佳做法)是在您的ItemViewController 中创建一个函数来捕获新的更改。

    func captureModified(itemStore: ItemStore) {
         self.itemStore = itemStore
    }
    

    接下来,当您从ItemViewController 转换到AddViewController 时,在AddViewController 内创建ItemViewController 的实例,并在您从ItemViewController 转换到AddViewController 时对其进行初始化。

    class AddViewController: UIViewController {
         weak var itemViewController: ItemViewController?
    }
    

    从ItemViewController 转换到AddViewController

    let addViewController = segue.destination as! AddViewController
    addViewController.itemViewController = self
    

    最后,当你过渡回来时;您可以在 deinit 方法中执行此操作,也可以在 AddViewController 方法中创建自己的返回方法。

    deinit {
         itemViewController.captureModified(itemStore: itemStore)
    }
    

    这是做事最简单的方法。最安全和最佳的做法是将captureModified(_:) 方法包装在协议中并创建该协议的实例而不是整个ItemViewController 类。如果您愿意,请告诉我,我可以实现它。或者,如果时间允许,我会不顾未来的读者。

    【讨论】:

    • 所以我不会只传递物品
    • 只更新第一行覆盖函数准备(对于segue:UIStoryboardSegue,发件人:Any?){ switch segue.identifier { case " added": let itemsViewController = segue.destination as! ItemsViewController itemsViewController.itemStore = itemStore default: preconditionFailure("Unexpected segue identifier.") } }
    【解决方案2】:

    我将新的segue 添加到AddViewController 回到itemViewController

       switch segue.identifier {
       case "added":
           
       let itemsViewController = segue.destination as! ItemsViewController
           itemsViewController.itemStore = itemStore
    
       default:
       preconditionFailure("Unexpected segue identifier.")
    
    
    
    

    跟踪itemsViewController中的单元格索引

     @IBAction func addNewItem(_ sender: UIButton) {
         // Create a new item and add it to the store
         let newItem = itemStore.createItem()
    
         // Figure out where that item is in the array
         if let index = itemStore.allItems.firstIndex(of: newItem) {
             let indexPath = IndexPath(row: index, section: 0)
             self.row = index
    
             // Insert this new row into the table
             tableView.insertRows(at: [indexPath], with: .automatic)
         }
        }
    
    
    

    并将正在更新的正确行传递给AddViewController,以便ItemViewController 知道正在更新的行

                
                let row = self.row
    
    //                // Get the item associated with this row and pass it along
                    let item = itemStore.allItems[row]
                let itemStore = self.itemStore;
                    let addViewController
                            = segue.destination as! AddViewController
                    addViewController.item = item
                addViewController.itemStore = itemStore
                addViewController.row = self.row;
    //            }
                break;
            default:
                preconditionFailure("Unexpected segue identifier.")
            }
    
    
    

    我还在AddViewController 的顶部添加了这个以拥有itemViewController weak var itemViewController: ItemsViewController? 的实例

    【讨论】:

      猜你喜欢
      • 2011-08-04
      • 1970-01-01
      • 2019-07-03
      • 2022-12-18
      • 2022-11-19
      • 1970-01-01
      • 2021-07-12
      • 1970-01-01
      • 2015-11-27
      相关资源
      最近更新 更多