如何将数据保存到tableview中的特定类别而不是所有类别?

时间:2016-08-08 03:12:29

标签: ios swift uitableview persistence uistoryboardsegue

我有一个shoppingList应用程序 当您将项目添加到类别列表时,它会添加到整个类别列表时不应该!

https://github.com/mrbryankmiller/Grocery-TableView-.git

class GroceryItemsTableViewController: UITableViewController {

    //var groceryItem = ["Item1", "Item2", "Item3"]

    //var groceryList  = ["Breakfast","Lunch", "Dinner"]


    @IBOutlet var groceryItemTableView: UITableView!


    @IBAction func addGroceryItemButtonPressed(sender: UIBarButtonItem) {

        ///new way///

        let alertController: UIAlertController = UIAlertController(title: "Add Grocery Item", message: "", preferredStyle: .Alert)

        //Cancel Button

        let cancelAction: UIAlertAction = UIAlertAction(title: "Cancel", style: .Cancel) { action -> Void in
            //cancel code
        }
        alertController.addAction(cancelAction)
        let saveAction: UIAlertAction = UIAlertAction(title: "Save", style: .Default) { action -> Void in

            let textField = alertController.textFields![0]
            groceryItem.items.append(textField.text!)
            self.tableView.reloadData()

        }

        alertController.addAction(saveAction)


        //Add text field

        //        alertController.addTextFieldWithConfigurationHandler { (textField) -> Void in
        //        textField.textColor = UIColor.blackColor()

        alertController.addTextFieldWithConfigurationHandler { (textField : UITextField!) -> Void in
            textField.placeholder = "Enter an Item"
            //alertController.textFields
        }

        //Present the AlertController
        self.presentViewController(alertController, animated: true, completion: nil)
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        //self.navigationItem.leftBarButtonItem = self.editButtonItem()

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    // MARK: - Table view data source

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows

        return groceryItem.items.count

    }


    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCellWithIdentifier("groceryItem1", forIndexPath: indexPath)
        cell.textLabel!.text = groceryItem.items [indexPath.row]
        return cell

    }
}

1 个答案:

答案 0 :(得分:0)

如果您仔细查看班级groceryItem的声明,您的购物清单中的每个项目都有static个元素数组,因此每次添加新元素时,它都会在所有杂货项目中共享。

相反,您应该为每个杂货商品添加与其每个商品相关联的列表。

您可以定义一个新的struct,为每个杂货项目保存其关联项目列表,如下所示:

struct GroceryItem {
   var name: String
   var items: [String]
}

我们将根据您的新模型更改GroceryListTableViewController中的代码以重构代码,因此它应如下所示:

  

<强> GroceryListTableViewController

class GroceryListTableViewController: UITableViewController, GroceryItemsTableViewControllerProtocol {


    var groceryList = [GroceryItem]()

    @IBAction func addButton(sender: UIBarButtonItem) {

       let alertController: UIAlertController = UIAlertController(title: "Add Grocery Category", message: "", preferredStyle: .Alert)

       //Cancel Button

       let cancelAction: UIAlertAction = UIAlertAction(title: "Cancel", style: .Cancel) { action -> Void in
        //cancel code
       }
       alertController.addAction(cancelAction)

       let saveAction: UIAlertAction = UIAlertAction(title: "Save", style: .Default) { action -> Void in

          let textField = alertController.textFields![0]
          self.groceryList.append(GroceryItem(name: textField.text!, items: [String]()))
          self.tableView.reloadData()
       }
       alertController.addAction(saveAction)

       alertController.addTextFieldWithConfigurationHandler { (textField : UITextField!) -> Void in
          textField.placeholder = "Enter an Item"
          //alertController.textFields
        }

       //Present the AlertController
       self.presentViewController(alertController, animated: true, completion: nil)
    }

    override func viewDidLoad() {

       super.viewDidLoad()

       //edit button
       self.navigationItem.leftBarButtonItem = self.editButtonItem()

       groceryList.append(GroceryItem(name: "Breakfast", items: ["Item1", "Item2",  "Item3"]))
       groceryList.append(GroceryItem(name: "Lunch", items: ["Item1", "Item2",  "Item3"]))
       groceryList.append(GroceryItem(name: "Dinner", items: ["Item1", "Item2",  "Item3"]))

    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

       return groceryList.count
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
       let cell = tableView.dequeueReusableCellWithIdentifier("prototype1", forIndexPath: indexPath) as UITableViewCell

       cell.textLabel!.text = groceryList [indexPath.row].name

       return cell
    }

   // pass a tableview cell value to navigationBar title in swift//

   override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
      let destinationVC = segue.destinationViewController as! GroceryItemsTableViewController
      let cell = sender as! UITableViewCell

      let idx = self.tableView.indexPathForSelectedRow?.row

      destinationVC.delegate = self
      destinationVC.itemList = groceryList[idx!].items
      destinationVC.navigationItem.title = cell.textLabel?.text
   }

   func didAddGroceryItem(itemName: String) {
      let idx = self.tableView.indexPathForSelectedRow?.row
      groceryList[idx!].items.append(itemName)
   }

   func didRemoveGroceryItem(index: Int) {
    let idx = self.tableView.indexPathForSelectedRow?.row
    groceryList[idx!].items.removeAtIndex(index)
   }  
}

在上面我已经重构了关于新模型的所有代码,我只把代码改变的地方保持不变。

您需要将与所选单元格相关联的项目传递给另一个UIViewController,您可以在prepareForSegue中轻松完成。为此,我们需要获取所选单元格的索引,并将元素传递给另一个UIViewController,其中我们创建了一个新的[String]数组作为数据源来显示项目。

代码中的另一个重点是GroceryListTableViewController现在实现了一个名为GroceryItemsTableViewControllerProtocol的新协议。这个协议是每次将新项目添加到列表时从GroceryListTableViewController通知GroceryItemsTableViewController的方式,称为委托模式。

  

<强> GroceryItemsTableViewController

protocol GroceryItemsTableViewControllerProtocol: class {

   func didAddGroceryItem(itemName: String)

   func didRemoveGroceryItem(index: Int)
}

class GroceryItemsTableViewController: UITableViewController {

     weak var delegate: GroceryItemsTableViewControllerProtocol?

     var itemList: [String]!

     @IBAction func addGroceryItemButtonPressed(sender: UIBarButtonItem) {

        ///new way///

       let alertController: UIAlertController = UIAlertController(title: "Add Grocery Item", message: "", preferredStyle: .Alert)

       //Cancel Button

       let cancelAction: UIAlertAction = UIAlertAction(title: "Cancel", style: .Cancel) { action -> Void in
          //cancel code
       }
       alertController.addAction(cancelAction)


       let saveAction: UIAlertAction = UIAlertAction(title: "Save", style: .Default) { [weak self] action -> Void in

          guard let s = self else { return }

          let textField = alertController.textFields![0]

          s.itemList.append(textField.text!)
          s.delegate?.didAddGroceryItem(textField.text!)
          s.tableView.reloadData()

       }

      alertController.addAction(saveAction)

      alertController.addTextFieldWithConfigurationHandler { (textField : UITextField!) -> Void in
         textField.placeholder = "Enter an Item"
        //alertController.textFields
      }

      //Present the AlertController
      self.presentViewController(alertController, animated: true, completion: nil)
  }

  override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
     return itemList.count
   }


  override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
      let cell = tableView.dequeueReusableCellWithIdentifier("groceryItem1", forIndexPath: indexPath)


      cell.textLabel!.text = itemList[indexPath.row]
      return cell

  }

  override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
    if editingStyle == .Delete {
        // Delete the row from the data source
        itemList.removeAtIndex(indexPath.row)
        delegate?.didRemoveGroceryItem(indexPath.row)
        tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
    } else if editingStyle == .Insert {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }
  }
}

修改 要正确处理删除,您应该创建一个新的委托方法,不要通知GroceryListTableViewController项目已被删除,然后将其正确删除,您可以在上面的更新代码中看到。

我希望这对你有所帮助。