在我的iOS應用程序中,我最喜歡的詞有DetailController和FavoriteViewController如何自動重新加載表格視圖後添加到收藏夾的單詞?如何從另一個控件視圖swift 3重新加載tableview?
這是有趣的數據發送到我的分貝DetailController
@IBAction func favs(_ sender: UIButton) {
if listMean[sender.tag].Favorite == 1 {
listMean[sender.tag].Favorite = 0
}
else {
listMean[sender.tag].Favorite = 1
}
Singleton.ShareInstance.dbHelper.updateFavorite(newMean: listMean[sender.tag])
}
這是我FavoriteViewController
import UIKit
class FavoriteViewController: UITableViewController {
var listFavorite = [FavoriteModel]()
var dbHelper = DatabaseHelper()
override func viewDidLoad() {
super.viewDidLoad()
loadAllWords()
}
func relodaTableview() {
self.tableView.reloadData()
}
func loadAllWords(){
listFavorite = dbHelper.getAllFavorite()
tableView.dataSource = self
tableView.delegate = self
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
navigationItem.title = nil
if segue.identifier == (Singleton.ShareInstance.enwordSelected?.Word){
navigationItem.title = (Singleton.ShareInstance.enwordSelected?.Word)
}
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return listFavorite.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! FavoriteViewCell
cell.favoriteword = listFavorite[indexPath.row]
return cell
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 45
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
Singleton.ShareInstance.favoriteSelected = listFavorite[indexPath.row]
let des = storyboard?.instantiateViewController(withIdentifier: "DetailController")
navigationController?.pushViewController(des!, animated: true)
}
}
使用委託方法 –