我有一个带有两个标签的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")
}
答案 0 :(得分:0)
func addToFavorites(row: Int) {
passedTitle = storyTableViewTitle[row]
passedText = storyTableViewText[row]
doneEditingRows()
}
通过查看上面的代码,似乎passedTitle
和passedText
只是普通的String变量,因此它们只保存最后选择的行,因为它们只能容纳一个值。
要选择多个值,您可以将这些变量更改为Array
你可以像这样在Swift中声明Array:
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
循环从数组中提取数据。
希望这会有所帮助。 :)