在编辑模式下进行多项选择并将数据传递到其他地方

Multiple selection in edit mode and passing the data elsewhere

我有一个带有两个选项卡(我的故事和收藏夹)的 Tabview。当我在“我的故事”中进入编辑模式时,我希望能够将 select 多个故事添加到收藏夹。我拥有的代码在一定程度上起作用。我可以 select 多个故事,但是当我按下按钮 "Add Favorite" 时,只有我 select 编辑的最后一个故事被添加到收藏夹。我不知道如何将所有 selected 的故事存储到收藏夹中。相关代码如下:

   func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        row = indexPath.row
        if tableView.cellForRowAtIndexPath(indexPath)?.selected == true {

        addToFavorites(row)

        }
    }
    func addToFavorites(row: Int) {
        passedTitle = storyTableViewTitle[row]
        passedText = storyTableViewText[row]
        doneEditingRows()
    }


    class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, addNewStoryViewControllerDelegate, editStoryTextDelegate, UITabBarControllerDelegate {
    @IBOutlet var tableView: UITableView!

    var storyTableViewTitle = [String]()
    var storyTableViewText = [String]()
    var row = Int()
     func addNewStory() {
        performSegueWithIdentifier("showNewStory", sender: self)
    }

    func addToFavorites() {
        passedTitle.append(storyTableViewTitle[row])
        passedText.append(storyTableViewText[row])
        doneEditingRows()
    }

    func editRows() {
        tableView.allowsMultipleSelectionDuringEditing = true
        if storyTableViewTitle != [] {
        tableView.editing = true
        navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Done, target: self, action: "doneEditingRows")
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Add Favorite", style: .Plain, target: self, action: "addToFavorites")
        } else {
            navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Edit, target: self, action: "editRows")
            navigationItem.leftBarButtonItem?.enabled = false
        }

    }
    func doneEditingRows() {

        tableView.editing = false
        navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Edit, target: self, action: "editRows")
        navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Add, target: self, action: "addNewStory")
        if storyTableViewTitle == [] {
            navigationItem.leftBarButtonItem?.enabled = false
        }
    }

    func tableView(tableView: UITableView, didEndEditingRowAtIndexPath indexPath: NSIndexPath) {
        tableView.editing = false
        navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Edit, target: self, action: "editRows")
        navigationItem.rightBarButtonItem?.enabled = true
        if storyTableViewTitle == [] {
            navigationItem.leftBarButtonItem?.enabled = false
        }
    }
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return storyTableViewTitle.count
    }

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

        cell.textLabel?.text = storyTableViewTitle[indexPath.row]
        cell.accessoryType = .DetailButton

        return cell
    }
    func tableView(tableView: UITableView, moveRowAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath) {

        let val = storyTableViewTitle.removeAtIndex(sourceIndexPath.row)
        let val1 = storyTableViewText.removeAtIndex(sourceIndexPath.row)

        storyTableViewTitle.insert(val, atIndex: destinationIndexPath.row)
        storyTableViewText.insert(val1, atIndex: destinationIndexPath.row)

    }
    func tableView(tableView: UITableView, willBeginEditingRowAtIndexPath indexPath: NSIndexPath) {
        navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Done, target: self, action: "doneEditingRows")
        navigationItem.rightBarButtonItem?.enabled = false
    }

    func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? {

        let deleteAction = UITableViewRowAction(style: .Destructive, title: "Delete") {(action, indexPath) in

            self.storyTableViewTitle.removeAtIndex(indexPath.row)
            self.storyTableViewText.removeAtIndex(indexPath.row)

            let defaults = NSUserDefaults.standardUserDefaults()

            defaults.setObject(self.storyTableViewText, forKey: "storyText")
            defaults.setObject(self.storyTableViewTitle, forKey:"storyTitle")
            tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)

            tableView.reloadData()

        }
        let favoriteAction = UITableViewRowAction(style: .Default, title: "Favorite") {(action, indexPath) in
            passedTitle.append(self.storyTableViewTitle[indexPath.row])
            passedText.append(self.storyTableViewText[indexPath.row])
            tableView.reloadData()
        }
        favoriteAction.backgroundColor = UIColor.greenColor()
        return [deleteAction,favoriteAction]
    }
    func tableView(tableView: UITableView, accessoryButtonTappedForRowWithIndexPath indexPath: NSIndexPath) {
        row = indexPath.row
        performSegueWithIdentifier("showStory", sender: self)
    }
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

        if tableView.cellForRowAtIndexPath(indexPath)?.selected == true {
            row = indexPath.row
        }
    }
    import UIKit

var passedTitle = [String]()
var passedText = [String]()

class FavoritesTableViewController: UITableViewController, editStoryTextDelegate {


    var favoriteTitles = [String]()
    var favoriteTexts = [String]()
    var row = Int()



   override func viewDidLoad() {
        super.viewDidLoad()



    let defaults = NSUserDefaults.standardUserDefaults()
    if let storyTextArray = defaults.objectForKey("favoriteStoryText") as? [String] {
        favoriteTexts = storyTextArray
    }
    if let storyTitleArray = defaults.objectForKey("favoriteStoryTitle") as? [String] {
        favoriteTitles = storyTitleArray
    }

    tableView.reloadData()



        navigationController?.navigationBar.barStyle = .Black
        navigationController?.navigationBar.tintColor = UIColor.redColor()
    navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.redColor()]

    navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Edit, target: self, action: "editRows")

    }
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)


        // use for defaults reset

//        favoriteTitles = []
//        favoriteTexts = []
//        let defaults = NSUserDefaults.standardUserDefaults()
//        defaults.setObject(favoriteTitles, forKey: "favoriteStoryTitle")
//        defaults.setObject(favoriteTexts, forKey: "favoriteStoryText")

        if passedTitle != [] {
            if passedText != [] {
                favoriteTitles.append(passedTitle[row])
                favoriteTexts.append(passedText[row])
                passedTitle = []
                passedText = []

                let defaults = NSUserDefaults.standardUserDefaults()

                defaults.setObject(self.favoriteTexts, forKey: "favoriteStoryText")
                defaults.setObject(self.favoriteTitles, forKey:"favoriteStoryTitle")
                tableView.reloadData()
            }
        }
        if favoriteTitles != [] {
            navigationItem.leftBarButtonItem?.enabled = true
        } else {
            navigationItem.leftBarButtonItem?.enabled = false
        }

    }
       //MARK: TabelView Delegates

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

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCellWithIdentifier("favoritesCell", forIndexPath: indexPath)
        cell.textLabel?.text = favoriteTitles[indexPath.row]
        return cell
    }
    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        row = indexPath.row
        tableView.deselectRowAtIndexPath(indexPath, animated: true)
        performSegueWithIdentifier("showFavoriteStory", sender: self)
    }
    override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
        if editingStyle == .Delete {
            favoriteTitles.removeAtIndex(indexPath.row)
            favoriteTexts.removeAtIndex(indexPath.row)
            tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)


            let defaults = NSUserDefaults.standardUserDefaults()

            defaults.setObject(self.favoriteTexts, forKey: "favoriteStoryText")
            defaults.setObject(self.favoriteTitles, forKey:"favoriteStoryTitle")


            tableView.reloadData()

        }
    }
    override func tableView(tableView: UITableView, moveRowAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath) {
        let val = favoriteTitles.removeAtIndex(sourceIndexPath.row)
        let val1 = favoriteTexts.removeAtIndex(sourceIndexPath.row)

        favoriteTitles.insert(val, atIndex: destinationIndexPath.row)
        favoriteTexts.insert(val1, atIndex: destinationIndexPath.row)
    }
    override func tableView(tableView: UITableView, didEndEditingRowAtIndexPath indexPath: NSIndexPath) {
        tableView.editing = false
        navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Edit, target: self, action: "editRows")
        navigationItem.rightBarButtonItem?.enabled = true
        if favoriteTitles == [] {
            navigationItem.leftBarButtonItem?.enabled = false
        }
    }
   override func tableView(tableView: UITableView, willBeginEditingRowAtIndexPath indexPath: NSIndexPath) {
        navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Done, target: self, action: "doneEditingRows")

    }
func addToFavorites(row: Int) {
passedTitle = storyTableViewTitle[row]
passedText = storyTableViewText[row]
doneEditingRows()
}

通过查看上面的代码,passedTitlepassedText 似乎只是普通的字符串变量,因此它们只保存最后的 selected 行,因为它们只能保存一个价值。 为了 select 多个值,您可以将这些变量更改为 Array

您可以像这样在 Swift 中声明数组:

var passedTitle = [String]() //Empty array of type String
var passedText = [String]()

以下函数将更改为:

func addToFavorites(row: Int) {
    passedTitle.append(storyTableViewTitle[row])
    passedText.append(storyTableViewText[row])
    doneEditingRows()
    }

因此,每次 select 一行时,上面的数组都会继续存储值。您可以使用 for 循环从数组中提取数据。

希望这对您有所帮助。 :)