簡體   English   中英

如何將數據添加到 tableView 並重新加載?

[英]How to add data to the tableView and reload?

我試圖通過從另一個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 中的 function 調用(准備):

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

我知道是什么導致了錯誤(favoritesDestinationVC 創建了一個 tableView 為 nil 的新實例),但我不知道如何解決它。 關於如何以這種方式向 tableView 添加條目(然后更新表格)而不會使我的應用程序崩潰的任何想法?

將您的商店 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.
        }
    }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM