gpt4 book ai didi

ios - 编辑模式下的多重选择并将数据传递到其他地方

转载 作者:行者123 更新时间:2023-11-29 01:20:57 26 4
gpt4 key购买 nike

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

   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 只是普通的字符串变量,因此它们只保存最后选定的行,因为它们只能保存一个值。为了选择多个值,您可以将这些变量更改为 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()
}

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

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

关于ios - 编辑模式下的多重选择并将数据传递到其他地方,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34609398/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com