0
我有两个tableView在我的项目中运行。我已经成功地从我的第一个tableView传递数据到第二个tableView控制器使用segue.I我试图保存我使用NSUserDefaults传递给secondViewController的数据。我下面的部分代码....保存TableView数组在indexPath在Swift
第一个VC:
var tableView: UITableView!
var DataArray = ["Bus","Helicopter","Truck","Boat","Bicycle","Motorcycle","Plane","Train","Car","S cooter","Caravan"]
var sendSelectedData = NSString()
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let indexPath = tableView.indexPathForSelectedRow!
let currentCell = tableView.cellForRow(at: indexPath) as UITableViewCell!
// print(indexPath)
// print(currentCell)
sendSelectedData = (currentCell!.textLabel?.text)! as String as NSString
performSegue(withIdentifier: "ShowDetails", sender: indexPath)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let selectedIndex = sender as! NSIndexPath
let currentCell = tableView.cellForRow(at: selectedIndex as IndexPath)!
self.sendSelectedData = (currentCell.textLabel?.text)! as String as NSString
if segue.identifier == "ShowDetails" {
let viewController = segue.destination as! SecondController
viewController.newItem = (self.sendSelectedData as String)
}
}
二VC:
var NewArray = [""]
var newItem: String = ""
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
NewArray.insert(newItem, at: Array.count)
self.tableView?.beginUpdates()
// NewArray.append(newItem)
let defaults = UserDefaults.standard
defaults.object(forKey: "NewArray")
self.tableView?.endUpdates()
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as UITableViewCell
cell.textLabel?.text = NewArray[(indexPath as NSIndexPath).row] as String
return cell
}
func insertRowsAtIndexPaths(indexPaths: [AnyObject], withRowAnimation animation: UITableViewRowAnimation) {
}
func reloadRowsAtIndexPaths(indexPaths: [AnyObject], withRowAnimation animation: UITableViewRowAnimation) {
}
我正在关注https://grokswift.com/uitableview-updates/文章,以实现该功能....
在此先感谢。
谢谢你的代码工作得很好,很容易理解。再次感谢.... – Joe