Ios Swift TableView自定义单元格数据在今天小部件中重新加载表格后消失
我创建了一个today扩展,其中包含带有自定义单元格的tableView,并用服务器上的数据填充了该表。现在,每当调用Web服务刷新数据并且必须重新加载表数据时,表中的数据就会消失。简而言之,每次我从“今日”面板切换到“通知”面板再切换回来,表中的数据都会消失。下面是我的控制器的代码 TodayViewController.swift:Ios Swift TableView自定义单元格数据在今天小部件中重新加载表格后消失,ios,swift,uitableview,ios9-today-widget,Ios,Swift,Uitableview,Ios9 Today Widget,我创建了一个today扩展,其中包含带有自定义单元格的tableView,并用服务器上的数据填充了该表。现在,每当调用Web服务刷新数据并且必须重新加载表数据时,表中的数据就会消失。简而言之,每次我从“今日”面板切换到“通知”面板再切换回来,表中的数据都会消失。下面是我的控制器的代码 TodayViewController.swift: import UIKit import NotificationCenter class TodayViewController: UIViewControl
import UIKit
import NotificationCenter
class TodayViewController: UIViewController, NCWidgetProviding, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var tableView: UITableView!
var key : [String]=[String]()
var value: [String]=[String]()
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
override func viewDidLoad() {
super.viewDidLoad()
dispatch_async(dispatch_get_main_queue(), { () -> Void in
print("This is run on the main queue, after the previous code in outer block")
self.callUrl()
self.tableView.reloadData()
})
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
print(key.count)
return key.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell=self.tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as!CustomCell
cell.key.text=key[indexPath.row]
cell.value.text=value[indexPath.row]
cell.setNeedsLayout()
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
}
func callUrl(){
let requestURL: NSURL = NSURL(string: "http://www.learnswiftonline.com/Samples/subway.json")!
let urlRequest: NSMutableURLRequest = NSMutableURLRequest(URL: requestURL)
let session = NSURLSession.sharedSession()
let task = session.dataTaskWithRequest(urlRequest) {
(data, response, error) -> Void in
let httpResponse = response as! NSHTTPURLResponse
let statusCode = httpResponse.statusCode
if (statusCode == 200) {
print("Everyone is fine, file downloaded successfully.")
self.key.removeAll()
self.value.removeAll()
do{
let json = try NSJSONSerialization.JSONObjectWithData(data!, options:.AllowFragments)
if let stations = json["stations"] as? [[String: AnyObject]] {
for station in stations {
if let name = station["stationName"] as? String {
if let year = station["buildYear"] as? String {
self.key.append(name)
self.value.append(year)
print(self.key)
}
}
}
}
}
catch {
print("Error with Json: \(error)")
}
}
}
task.resume()
}
func widgetPerformUpdateWithCompletionHandler(completionHandler: ((NCUpdateResult) -> Void)) {
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.callUrl()
self.tableView.reloadData()
})
completionHandler(NCUpdateResult.NewData)
}
}
包含数据的表格:
没有数据的表格:
我不熟悉ios和swift。任何帮助都会很好!谢谢。您应该在callURL()方法的成功块中重新加载tableview。您应该在callURL()方法的成功块中重新加载tableview。谢谢。但是现在,当视图最初加载时,它也是空的。有什么建议吗?谢谢。但是现在,当视图最初加载时,它也是空的。有什么建议吗?