2016-10-02 110 views
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/文章,以實現該功能....

在此先感謝。

回答

1

AppDelegateapplicationDidFinishLaunching註冊一個空數組作爲鍵「NewArray」的默認值。

let defaultValues = ["NewArray": [String]()] 
UserDefaults.standard.register(defaults: defaultValues) 

二VC限定newArray(變量名應該開始以小寫字母),也爲空String陣列

var newArray = [String]() 

viewDidLoad檢索來自用戶的默認值的陣列,追加新項目,將陣列保存回用戶默認值並重新載入表格視圖

override func viewDidLoad() { 
    super.viewDidLoad() 

    let defaults = UserDefaults.standard 
    newArray = defaults.array(forKey: "NewArray") as! [String] 
    newArray.append(newItem) 
    defaults.set(newArray, forKey: "NewArray") 
    self.tableView?.reloadData() 
} 

beginUpdates(),endUpdates()insertRowsAtIndexPath/reloadRowsAtIndexPaths根本不需要。

+0

謝謝你的代碼工作得很好,很容易理解。再次感謝.... – Joe