如何更新tableview中的数据(Swift) [英] how to update data in tableview (Swift)
问题描述
我有两个Swift文件:NotificationsViewController和ViewController。
点击按钮时,在ViewContoller中更新firstArray。我能够打印更新的数据。但是,当我切换回NotificationsViewController时,当我拉到刷新时,tableview没有更新。
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.
NotificationsViewController:
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:
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)
}
}
我试过这个但它也没有用。
更新:
如何更新基于另一个数组的cell.detailTextLabel?.text的值?
How can I update a value of cell.detailTextLabel?.text that is based on another array?
提前谢谢
推荐答案
一个问题:
更新后第一个数组。您还需要更新数据。它不会自动更新。所以你的代码看起来像
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()
}
反馈:
不使用类外的变量,更好用单身人士班。它看起来像这样:
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 = []
}
您可以像
Singleton.sharedInstance.firstArray
这篇关于如何更新tableview中的数据(Swift)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!