本文介绍了在我点击另一个选项卡并返回之前,tableView 不会更新 - swift的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 iOS 新手,我通过解析 JSON 数据从网络源动态填充 tableView 数据.但问题是当我第一次访问该选项卡时,它什么也没显示.在我访问另一个选项卡并返回后,它在 tableView 上正确显示数据.

I'm new in iOS , i'm populating tableView data dynamically from a web source by parsing JSON data . But the problem is when i visit that tab first time its showing nothing . After i visit another tab and get back , its showing the data properly on tableView .

这是我的代码

import UIKit

class BranchViewController: UITableViewController , UISearchBarDelegate, UISearchDisplayDelegate {

    var branches = [Branch]()
    var filteredBranch = [Branch]()
    var BranchArray = [BranchTable]()
    var BranchLocationArray = [BranchLocation]()


    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        self.tableView!.delegate = self
        self.tableView!.dataSource = self

        let url = NSURL(string: "http://rana.ninja/branches.php")

        let task = NSURLSession.sharedSession().dataTaskWithURL(url!) {(data, response, error) in
            println(NSString(data: data, encoding: NSUTF8StringEncoding))
            var error: NSError?
            let jsonData: NSData = data /* get your json data */
            let json = NSJSONSerialization.JSONObjectWithData(jsonData, options: nil, error: &error) as NSDictionary

            if let name: AnyObject = json["name"]  {
                for var index = 0; index < name.count; ++index {
                    var Name : String  = name[index] as NSString
                    self.branches.append(Branch(name: Name))
                    println(Name)
                }
            }
        }

        task.resume()

        BranchArray =
            [BranchTable(BranchTitle: ["FirstFirst","SecondFirst","ThirdFirst"]),
                BranchTable(BranchTitle: ["FirstSecond","SecondSecond","ThirdSecond"]),
                BranchTable(BranchTitle: ["FirstThird","SecondThird","ThirdThird"]),
                BranchTable(BranchTitle: ["FirstFourth"])]

        BranchLocationArray = [BranchLocation(BranchLocationValues: ["asdfasd","asdfasd","asdfas"]),
            BranchLocation(BranchLocationValues: ["asdkljf","asdfasd","asdfas"]),
            BranchLocation(BranchLocationValues: ["asdkljf","asdfasd","asdfas"]),
            BranchLocation(BranchLocationValues: ["asdkljf","asdfasd","asdfas"])]

            // Reload the table
            self.tableView.reloadData()
            }

推荐答案

更新数据结构后需要调用tableView.reloadData.这必须在主线程中完成,因此:

You need to call tableView.reloadData after you have updated the data structures. This has to be done in the main thread, thus:

 let task = NSURLSession.sharedSession().dataTaskWithURL(url!) {(data, response, error) in
     println(NSString(data: data, encoding: NSUTF8StringEncoding))
     var error: NSError?
     let jsonData: NSData = data /* get your json data */
     let json = NSJSONSerialization.JSONObjectWithData(jsonData, options: nil, error: &error) as NSDictionary

     if let name: AnyObject = json["name"]  {
         for var index = 0; index < name.count; ++index {
             var Name : String  = name[index] as NSString
             self.branches.append(Branch(name: Name))
             println(Name)
         }
     }
     // NOTE: Calling reloadData in the main thread
     dispatch_async(dispatch_get_main_queue()) {
         self.tableView.reloadData()
     }
}

这篇关于在我点击另一个选项卡并返回之前,tableView 不会更新 - swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 19:25