ios - 在表格 View 中重新加载和保存项目

标签 ios swift uitableview reloaddata

所以我正在创建一个表格 View ,在其中添加(和删除)项目。我可以添加和删除 tableView 中的项目,但是...存在一些问题...

  1. 当我添加一个项目时,它不会显示在 tableView 中,直到我关闭模拟器,但当我关闭并重新启动应用程序时,它会显示在 tableView 中。

  2. 当我向 tableView 添加另一个项目并关闭应用程序时,旧项目将被新添加的项目替换。因此,当新项目添加到 tableView 时,会自动删除旧项目。

    当然,我想在关闭应用程序时保存现有项目,并且我想在现有 tableView 项目之上添加新项目,而不必关闭应用程序并重新启动...我不确定出了什么问题用我的代码...?预先感谢您!

表格 View

import UIKit

class PlaceTableVC : UITableViewController {

var placeList = [String]()

override func viewDidLoad() {
    super.viewDidLoad()

    if NSUserDefaults.standardUserDefaults().objectForKey("list")  != nil {
        placeList = NSUserDefaults.standardUserDefaults().objectForKey("list") as![String]

    }

    self.tableView.delegate = self


}

// MARK: - Table view data source

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {

    return 2
}


// Define number of different cells in the tableview

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if(section == 0) {
        return 1
    }
    else {
        return placeList.count
    }
}


// Define number of different cells in the tableview

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    switch indexPath.section{
    case 0:
        let myProfile = tableView.dequeueReusableCellWithIdentifier("AddAPlaceButton") as! TableViewCell

        return myProfile
    default:
        let cell = tableView.dequeueReusableCellWithIdentifier("AddedPlaceCell") as! SecondTableViewCell

        cell.textLabel?.text = placeList[indexPath.row]

        return cell
    }  
}


override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {

    let section = indexPath.section
    if(section == 0) {
        return 60
    }
    else {
        return 60
    }
}

override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath)
{
    if editingStyle == UITableViewCellEditingStyle.Delete {
        placeList.removeAtIndex(indexPath.row)
        NSUserDefaults.standardUserDefaults().setObject(placeList, forKey: "list")
        tableView.reloadData()
    }
    func viewDidAppear(animated: Bool) {
        tableView.reloadData()  
    }
}

}  

添加商品代码

import UIKit

var placeList = [String]()

class AddAPlaceModalVC: UIViewController, UITextFieldDelegate {

@IBOutlet var textField: UITextField!

@IBAction func addItem(sender:AnyObject) {
    placeList.append(textField.text!)
    textField.text = ""

    NSUserDefaults.standardUserDefaults().setObject(placeList, forKey: "list")

}


override func viewDidLoad() {
    super.viewDidLoad()
    self.textField.delegate = self
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
    self.view.endEditing(true)
}

func textFieldShouldReturn(textField: UITextField) -> Bool {
    textField.resignFirstResponder()
    return true

}

@IBAction func closeAddAPlaceVC(sender: AnyObject) {
    self.dismissViewControllerAnimated(true, completion: nil)

} 
}

最佳答案

在您的“PlaceTable VC”类中,尝试放置此函数:

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)
    self.tableView.reloadData()
}

此函数删除 viewDidiAppear:

override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath)
{
    if editingStyle == UITableViewCellEditingStyle.Delete {
        placeList.removeAtIndex(indexPath.row)
        NSUserDefaults.standardUserDefaults().setObject(placeList, forKey: "list")
        tableView.reloadData()
    }
}

关于ios - 在表格 View 中重新加载和保存项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35246548/

相关文章:

ios - 编辑和滚动时 UITableViewCell 内容不正确

ios - 如何合并两个 SwiftyJSON 对象

iOS/Swift - UITableView reloadData 未被调用

ios - 自动布局设置六个方形图像

ios - ASIHTTP请求委托(delegate)

ios - 向上滚动 tableView 的无限滚动?

ios - QuickBlox 自定义表 -- 在 iOS 上创建关系并获取相关记录

ios - 动态对比文本颜色

swift - 未调用解析 SignUpViewController signUpViewControllerShouldSignBeginSignUp 委托(delegate)函数

ios - "No rows found"当没有 UICollectionView 和 UITableView 的行/单元格时