简体   繁体   中英

Passing data from a UITableView cell to another VC's UITableView? (Swift)

I'm basically trying to pass data from a UITableView's cell, from one viewcontroller to my second viewcontroller when deleting a cell from the first viewcontroller.

Here's the code for my first viewcontroller class trying to pass the tableview data to an Array in my other viewcontroller class:

var deletedProperty: [String] = []
var storedDeletion: [String] = []

func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {

    if(editingStyle == UITableViewCellEditingStyle.Delete){
        storedDeletion.append(deletedProperty[indexPath.row])
        taskMgr.tasks.removeAtIndex(indexPath.row)
        tblTasks.reloadData();
    }

}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "YourIdentifier" {
        let destination = segue.destinationViewController as ThirdViewController; destination.secondTaskMgr = storedDeletion
    }
}

Here's the code for my other viewcontroller class containing the Array:

var secondTaskMgr: SecondTaskManager = SecondTaskManager()

struct secondTask {
    var passedName = ""
}

class SecondTaskManager: NSObject {
    var secondTasks = [secondTask]()

    func secondAddTask(name: String){
        secondTasks.append(secondTask(passedName: name))
    }
}

In the first viewcontroller class I get the error

'NSArray' is not a subtype of 'ThirdViewController.SecondTaskManager'" 

at

let destination = segue.destinationViewController as ThirdViewController;    
destination.secondTaskMgr = storedDeletion

What's the issue here and how do I fix it? And is there any better way to do this?

secondTaskMgr is a SecondTaskManager .

storedDeletion is a [String] .

You're trying to assign a [String] to a SecondTaskManager property.

You can't do that.

You can use NSUserDefaults .

FirstViewController.swift

let param: String = "ParamOne"
NSUserDefaults.standardUserDefaults().setObject(param, forKey:"paramId")
NSUserDefaults.standardUserDefaults().synchronize()

SecondViewController.swift

let param = NSUserDefaults.standardUserDefaults().stringForKey("paramId")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM