ios 如何更新 tableview (Swift) 中的数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37146801/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to update data in tableview (Swift)
提问by Jana
I have two Swift files: NotificationsViewController and ViewController. The firstArray is updated in the ViewContoller when a button is tapped. I was able to print the updated data. However, when I switch back to NotificationsViewController the tableview is not updated when I pull to refresh.
我有两个 Swift 文件:NotificationsViewController 和 ViewController。当点击按钮时,第一个数组会在 ViewContoller 中更新。我能够打印更新的数据。但是,当我切换回 NotificationsViewController 时,当我拉动刷新时,tableview 不会更新。
NotificationsViewController:
通知视图控制器:
import Foundation
import UIKit
var firstArray : [String] = ["123","1234"]
class NotificationsViewController: UITableViewController {
var refresher: UIRefreshControl!
var data: [[String]] = [firstArray, ["4","5"]]
func refresh(){
print("refreshed")
self.tableView.reloadData()
self.refresher.endRefreshing()
}
override func viewDidLoad() {
refresher = UIRefreshControl()
refresher.attributedTitle = NSAttributedString(string: "pull to refresh")
refresher.addTarget(self, action: #selector(NotificationsViewController.refresh), forControlEvents: UIControlEvents.ValueChanged)
self.tableView.addSubview(refresher)
refresh()
super.viewDidLoad()
self.tableView.editing = true
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return data.count
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data[section].count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell: UITableViewCell = UITableViewCell(style: .Subtitle, reuseIdentifier: "tableCell")
cell.textLabel?.text = data[indexPath.section][indexPath.row]
return cell
}
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == .Delete {
data[indexPath.section].removeAtIndex(indexPath.row)
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
}
}
}
ViewController:
视图控制器:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func button(sender: AnyObject) {
firstArray.append("522")
print(firstArray)
}
}
I tried thisbut it did not work too.
我试过这个,但它也不起作用。
Updated :
更新 :
How can I update a value of cell.detailTextLabel?.text that is based on another array?
如何更新基于另一个数组的 cell.detailTextLabel?.text 的值?
Thank you in advance
先感谢您
回答by Subash
One Issue:
一期:
After you update the first array. You need to update the data too. It doesn't get updated automatically. so your code could look like
更新第一个数组后。您也需要更新数据。它不会自动更新。所以你的代码看起来像
func refresh(){
data = [firstArray, ["4","5"]]
print("refreshed")
self.tableView.reloadData()
self.refresher.endRefreshing()
}
Feedback:
回馈:
Instead of using a variable outside of classes, its better to use a singleton class. It looks like this:
与其在类之外使用变量,不如使用单例类。它看起来像这样:
class Singleton: NSObject {
static let sharedInstance = Singleton()
var firstArray = []
}
You can update/retrive the array like
您可以像这样更新/检索数组
Singleton.sharedInstance.firstArray