我已经浏览过有关此主题的各种教程和帖子,但我似乎无法为我的项目制定解决方案。从本质上讲,我正在努力实现一个"拉动刷新"在iOS上的TableView中,然后通过Web服务器重新加载一些数据,然后使用最新的数据集重新加载我的表。
从测试开始,似乎我的"拉动刷新"正在工作,其中我成功地从我的Web服务请求中提取最新数据。然而,似乎失败的是表格的单元格实际上并没有使用最新数据进行更新。这是我在 TableViewController ;
中的内容import UIKit
class MinionsTableViewController: UITableViewController {
let minions: [Minion]
required init(coder aDecoder: NSCoder!) {
minions = Minion.fetchMinionData()
super.init(coder: aDecoder)
}
override func viewDidLoad() {
super.viewDidLoad()
let defaults = NSUserDefaults(suiteName: "group.com.example.myapp.data")
NSKeyedArchiver.setClassName("Minion", forClass: Minion.self)
defaults?.setObject(NSKeyedArchiver.archivedDataWithRootObject(minions), forKey: "minions")
self.refreshControl?.addTarget(self, action: "handleRefresh:", forControlEvents: UIControlEvents.ValueChanged)
}
func handleRefresh(refreshControl: UIRefreshControl) {
let minions: [Minion]
minions = Minion.fetchMinionData()
for minion in minions {
println(minion.now!)
}
self.tableView.reloadData()
refreshControl.endRefreshing()
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return minions.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("minionCell", forIndexPath: indexPath) as! UITableViewCell
let minion = minions[indexPath.row]
if let name = minion.name {
cell.imageView?.image = UIImage(named: minion.name!)
cell.textLabel?.text = minion.name ?? "Minion"
cell.detailTextLabel?.text = "Data: \(minion.now!)"
} else {
cell.imageView?.image = UIImage(named: "Dave")
cell.textLabel?.text = "No data available."
cell.detailTextLabel?.text = "Sorry, nothing to present here!"
}
return cell
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "ShowDetail" {
let indexPath = self.tableView.indexPathForSelectedRow()
//println(indexPath)
let minionDetail = minions[indexPath!.row]
let controller = (segue.destinationViewController as! UINavigationController).topViewController as! DetailedViewController
controller.detailItem = minionDetail
}
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
if let selectedIndexPath = self.tableView.indexPathForSelectedRow() {
self.tableView.deselectRowAtIndexPath(selectedIndexPath, animated: true)
}
}
}
我的" minion.now!"变量是一些带有当前时间戳的JSON数据,从我的println输出中,我可以看到它实际上在我拉动刷新时更新。但是,我的表格单元格似乎根本没有更新。
任何想法都会非常感激。谢谢!
答案 0 :(得分:0)
在下面的方法中,您将创建一个局部变量,该变量与您的类属性没有任何关系。
func handleRefresh(refreshControl: UIRefreshControl) {
let minions: [Minion]//Delete this line
minions = Minion.fetchMinionData() //self.minions = Minion.fetchMinionData()
for minion in minions {
println(minion.now!)
}
self.tableView.reloadData()
refreshControl.endRefreshing()
}
所以将行转换为
self.minions = Minion.fetchMinionData()
并且您在类级别声明的属性使其像
var minions: [Minion]
完整代码..
import UIKit
class MinionsTableViewController: UITableViewController {
var minions: [Minion]
required init(coder aDecoder: NSCoder!) {
minions = Minion.fetchMinionData()
super.init(coder: aDecoder)
}
override func viewDidLoad() {
super.viewDidLoad()
let defaults = NSUserDefaults(suiteName: "group.com.example.myapp.data")
NSKeyedArchiver.setClassName("Minion", forClass: Minion.self)
defaults?.setObject(NSKeyedArchiver.archivedDataWithRootObject(minions), forKey: "minions")
self.refreshControl?.addTarget(self, action: "handleRefresh:", forControlEvents: UIControlEvents.ValueChanged)
}
func handleRefresh(refreshControl: UIRefreshControl) {
self.minions = Minion.fetchMinionData()
for minion in minions {
println(minion.now!)
}
self.tableView.reloadData()
refreshControl.endRefreshing()
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return minions.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("minionCell", forIndexPath: indexPath) as! UITableViewCell
let minion = minions[indexPath.row]
if let name = minion.name {
cell.imageView?.image = UIImage(named: minion.name!)
cell.textLabel?.text = minion.name ?? "Minion"
cell.detailTextLabel?.text = "Data: \(minion.now!)"
} else {
cell.imageView?.image = UIImage(named: "Dave")
cell.textLabel?.text = "No data available."
cell.detailTextLabel?.text = "Sorry, nothing to present here!"
}
return cell
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "ShowDetail" {
let indexPath = self.tableView.indexPathForSelectedRow()
//println(indexPath)
let minionDetail = minions[indexPath!.row]
let controller = (segue.destinationViewController as! UINavigationController).topViewController as! DetailedViewController
controller.detailItem = minionDetail
}
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
if let selectedIndexPath = self.tableView.indexPathForSelectedRow() {
self.tableView.deselectRowAtIndexPath(selectedIndexPath, animated: true)
}
}
}