Swift3:从自定义表格视图单元格类中重新绘制表格视图

时间:2017-01-05 17:41:54

标签: ios uitableview swift3 uikit

我有一个包含表视图的视图控制器。在表视图中是具有用于删除单元格包含的数据的按钮的单元格。当我按下删除按钮时,单元格信息将从我的数据库中删除,但表格视图不会更新,直到我来回切换。

我不确定如何访问TableView并强制它从表格单元格中的方法重新绘制自己。

class EditTaskTableViewCell: UITableViewCell {

   //......//

    @IBAction func deleteButtonPressed(_ sender: Any) {
        let userDefaults = Foundation.UserDefaults.standard
        if let userTasks = userDefaults.array(forKey: "userTasks"){
            let newArray = removeTask(item: taskNameLabel.text!, from: userTasks as! [String])
            userDefaults.set(newArray, forKey: "userTasks")
            userDefaults.synchronize()
        }

        //update table view 

    }
}


class EditTaskTable_VC: UIViewController, UITableViewDelegate, UITableViewDataSource {

   //......//

   func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

           let cell = Bundle
           .main
           .loadNibNamed("EditTaskTableViewCell", owner: self, options: nil)?
           .first as! EditTaskTableViewCell

          cell.taskName.text = cellData[indexPath.row].title 

          return cell       

   }

   func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
       return cellData.count
   }

1 个答案:

答案 0 :(得分:0)

尝试使用委托。

这个例子是在目标C中,但总体思路就在那里。

sending reload data from custom tableview cell?

swift delegate示例

https://gist.github.com/austinzheng/db58036c4eb825e63e88

相关问题