通过展开segue

时间:2017-08-16 02:54:04

标签: ios swift

我在使用此代码在集合视图中添加新餐时遇到问题。当我添加一个新的并且segue展开时,它只替换另一个单元而不是在集合视图中创建一个新单元

当我在表格视图中使用此代码时,它的功能正常但我已转移到集合中,无法使其正常工作。

    @IBAction func unwindToMealList(sender: UIStoryboardSegue) {
    if let sourceViewController = sender.source as? MealViewController, let meal = sourceViewController.meal {

        if let selectedIndexPath = collectionView?.indexPathsForSelectedItems {
            // Update an existing meal.
            meals[selectedIndexPath.count] = meal
            collectionView!.reloadData()
        }
        else {

            // Add a new meal.

            let newIndexPath = IndexPath(item: meals.count, section: 0)
            print(newIndexPath)
            print(meals.count)
            meals.append(meal)
            collectionView!.insertItems(at: [newIndexPath])

             }
        saveMeals()


        }
    }
}

MEAL COLLECTION VIEW CONTROLLER

import UIKit
import os.log

private let reuseIdentifier = "Cell"

class MealCollectionViewController: UICollectionViewController {

//MARK: Properties

var meals = [Meal]()

override func viewDidLoad() {
    super.viewDidLoad()

    // Use the edit button item provided by the table view controller.
    navigationItem.leftBarButtonItem = editButtonItem

    // Load any saved meals, otherwise load sample data.
    if let savedMeals = loadMeals() {
        meals += savedMeals

    }
    else {
        // Load the sample data.
        loadSampleMeals()
    }
}

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

//MARK: - Table view data source

override func numberOfSections(in collectionView: UICollectionView) -> Int {
    return 1
}

override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return meals.count
}


override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    // Table view cells are reused and should be dequeued using a cell identifier.
    let cellIdentifier = "MealCollectionViewCell"

    guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellIdentifier, for: indexPath) as? MealCollectionViewCell  else {
        fatalError("The dequeued cell is not an instance of MealCollectionViewCell.")
    }

    // Fetches the appropriate meal for the data source layout.
    let meal = meals[indexPath.item]

    cell.photoImageView.image = meal.photo


    return cell
}





/*
 // Override to support rearranging the table view.
 override func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to: IndexPath) {

 }
 */

/*
 // Override to support conditional rearranging of the table view.
 override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
 // Return false if you do not want the item to be re-orderable.
 return true
 }
 */

//MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    super.prepare(for: segue, sender: sender)
    switch(segue.identifier ?? "") {
    case "AddItem":
        os_log("Adding a new meal.", log: OSLog.default, type: .debug)
    case "ShowDetail":
        guard let mealDetailViewController = segue.destination as? MealViewController else {
            fatalError("Unexpected destination: \(segue.destination)")
        }

        guard let selectedMealCell = sender as? MealCollectionViewCell else {
            fatalError("Unexpected sender: \(String(describing: sender))")
        }

        guard let indexPath = collectionView?.indexPath(for: selectedMealCell) else {
            fatalError("The selected cell is not being displayed by the table")
        }

        let selectedMeal = meals[indexPath.row]
        mealDetailViewController.meal = selectedMeal


    default:
        fatalError("Unexpected Segue Identifier; \(segue.identifier)")

    }




}

//MARK: Private Methods

private func loadSampleMeals() {

    let photo1 = UIImage(named: "meal1")
    let photo2 = UIImage(named: "meal2")
    let photo3 = UIImage(named: "meal3")

    guard let meal1 = Meal(name: "Caprese Salad", photo: photo1, method: "", ingredients: "") else {
        fatalError("Unable to instantiate meal1")
    }

    guard let meal2 = Meal(name: "Chicken and Potatoes", photo: photo2, method: "", ingredients: "") else {
        fatalError("Unable to instantiate meal2")
    }

    guard let meal3 = Meal(name: "Pasta with Meatballs", photo: photo3, method: "", ingredients: "") else {
        fatalError("Unable to instantiate meal2")
    }

    guard let meal4 = Meal(name: "Hello", photo: photo3, method: "", ingredients: "") else {
        fatalError("Unable to instantiate meal2")
    }


    meals += [meal1, meal2, meal3, meal4]
}

private func saveMeals() {
    let isSuccessfulSave = NSKeyedArchiver.archiveRootObject(meals, toFile: Meal.ArchiveURL.path)
    if isSuccessfulSave {
        os_log("Meals successfully saved.", log: OSLog.default, type: .debug)
    } else {
        os_log("Failed to save meals...", log: OSLog.default, type: .error)
    }
}

private func loadMeals() -> [Meal]? {
    return NSKeyedUnarchiver.unarchiveObject(withFile: Meal.ArchiveURL.path) as? [Meal]
}


//MARK: Actions
@IBAction func unwindToMealList(sender: UIStoryboardSegue) {
    if let sourceViewController = sender.source as? MealViewController, let meal = sourceViewController.meal {

        if let selectedIndexPath = collectionView?.indexPathsForSelectedItems {
            // Update an existing meal.
            meals[selectedIndexPath.count] = meal
            collectionView!.reloadData()
        }
        else {

            // Add a new meal.

            let newIndexPath = IndexPath(item: meals.count, section: 0)
            meals.append(meal)
            collectionView!.insertItems(at: [newIndexPath])

             }
        saveMeals()


        }
    }
}

0 个答案:

没有答案