【问题标题】:How to add data to the tableView and reload?如何将数据添加到 tableView 并重新加载?
【发布时间】:2020-12-21 18:42:37
【问题描述】:

我正在尝试通过从另一个 viewController 的 segue 发送数据,将单元格添加到 viewController 中的 tableView。

class FavoritesViewController: UIViewController {
    
    var shops = [
        "hello world",
        "hello world",
        "hello world"
    ]
    
    @IBOutlet weak var table: UITableView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        table.delegate = self
        table.dataSource = self
        table.reloadData()
    }
    
}

//protocol FavoritesDelegate: class {
//    func add(_ shopName: String)
//}

extension FavoritesViewController: UITableViewDelegate, UITableViewDataSource{
    
    func add(_ shopName: String) {
        print(shopName)
        shops.append(shopName)
    }
    
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return shops.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell",for: indexPath)
        cell.textLabel?.text = shops[indexPath.row]
        return cell
        
    }
    
    // define the action. In this case "delete"
    func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle {
        return .delete
    }
    
    // do the actual deleting
    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
        
        if editingStyle == .delete {
            tableView.beginUpdates()
            
            shops.remove(at: indexPath.row)
            tableView.deleteRows(at: [indexPath], with: .fade)
            
            tableView.endUpdates()
        }
        

    }
    
    
}

这是另一个 viewController 中的函数调用(准备):

 override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    
   
        favoritesDestinationVC = segue.destination as! FavoritesViewController
        favoritesDestinationVC.add(shopName!)
        
        
    
}

我知道导致错误的原因(favoritesDestinationVC 创建了一个 tableView 为 nil 的新实例),但我不知道如何解决它。关于如何以这种方式向 tableView 添加条目(然后更新表格)而不会使我的应用崩溃的任何想法?

【问题讨论】:

  • 使用闭包作为 UI 实例的回调。在另一个视图控制器中完成您的工作,在返回第一个视图控制器之前,只需调用第一个 VC 的回调即可。您提供的代码不足以向您建议一种有效的方法。您最好分享与您的问题相关的所有代码。
  • 我更新了代码。

标签: ios swift tableview segue reloaddata


【解决方案1】:

将您的商店 var 公开,然后在 segue 准备回调中直接使用它。首先检查变量是否具有除 nil 之外的有效值,如果存在则继续避免崩溃。您可以使用 if 语句进行验证,如您在示例代码中所见。或者您可以使用选项来避免崩溃。请参阅下面的代码。

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    if segue.destination is FavoritesViewController {
        let destinationVC = segue.destination as? FavoritesViewController // Use optionals which has ? (question mark)
        // To avoid crashes check if shopName has a valid value other than nil
        if let newShopName = shopName {
            // It is possible that the "shopName" has a nil value when the program reaches here.
            // That's why we will use the verified "newShopName" instead of "shopName" itself to avoid any crash.
            destinationVC?.shops.append(newShopName) // What I recommend, if not convenient for you just delete this line,
            // destinationVC?.add(newShopName) // after deleting the recommended line, uncomment this line for your convenience.
        }
    }

}

【讨论】:

    猜你喜欢
    • 2011-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多