V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
iOS 开发实用技术导航
NSHipster 中文版
http://nshipster.cn/
cocos2d 开源 2D 游戏引擎
http://www.cocos2d-iphone.org/
CocoaPods
http://cocoapods.org/
Google Analytics for Mobile 统计解决方案
http://code.google.com/mobile/analytics/
WWDC
https://developer.apple.com/wwdc/
Design Guides and Resources
https://developer.apple.com/design/
Transcripts of WWDC sessions
http://asciiwwdc.com
Cocoa with Love
http://cocoawithlove.com/
Cocoa Dev Central
http://cocoadevcentral.com/
NSHipster
http://nshipster.com/
Style Guides
Google Objective-C Style Guide
NYTimes Objective-C Style Guide
Useful Tools and Services
Charles Web Debugging Proxy
Smore
touzi
V2EX  ›  iDev

UITableViewController 用 Alamofire 加载网络数据, tableView 加载数据过程是什么?

  •  
  •   touzi · 2016-01-08 13:56:39 +08:00 · 2282 次点击
    这是一个创建于 3036 天前的主题,其中的信息可能已经有所发展或是发生改变。

    场景:从上一个 tableView 点击传入 uid ,然后在 viewDidLoad 里面做数据查询,最后展示到 tableView 中。
    问题:用 Alamofire 做 GET 请求得到结果,但在 Alamofire 打印结果条数为 0 ,在 Alamofire 内打印有结果条数。具体请看代码

    class SealViewController: UITableViewController {
    
        var userId: String!
        var seals: [SealModel] = []
    
        override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view, typically from a nib.
            print("uid->", userId)
            //加在公告 json 数据
            Alamofire.request(.GET, SERVICEAPIDOMAIN + "/Mobile/MobileHandler.ashx?method=toAudit&uid=" + userId).responseJSON {
                response in
                if let apiData = response.result.value {
                    let json = JSON(apiData)
                    for var i = 0; i < json["items"].count; i++ {
                        let line = json["items"][i]
                        self.seals.append(SealModel(id: line["ID"].stringValue, title: line["TitleStr"].stringValue, content: line["ContentStr"].stringValue, dateTime: line["TimeStr"].stringValue, type: line["Type"].stringValue))
                    }
                }
                print("Alamofire-sealsCount->", self.seals.count)
            }
    
            tableView.reloadData()
            print("sealsCount->", self.seals.count)
    
        }
    
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
    
    }
    

    打印输出结果

    uid-> 69
    sealsCount-> 0
    Alamofire-sealsCount-> 12
    

    如何让才能让 sealsCount 打印输出的时候就有结果?

    4 条回复    2016-12-29 15:16:09 +08:00
    Keita1314
        1
    Keita1314  
       2016-01-08 14:08:16 +08:00   ❤️ 2
    viewDidLoad 是 View 加载完成之后执行的函数,网络请求是异步行为,你在请求完成之后才能获取得数据,但此时 viewDiDLoad 已经执行完毕了,需要展示到 tableView ,可以在获取网络请求的响应之后再 reloadData
    touzi
        2
    touzi  
    OP
       2016-01-08 14:24:10 +08:00
    @Keita1314 感谢!
    free9fw
        3
    free9fw  
       2016-01-09 10:42:43 +08:00
    reloadData 应写在请求回调里
    fork3rt
        4
    fork3rt  
       2016-12-29 15:16:09 +08:00
    遇到了同样的问题, 感谢楼主和 1,3 楼~~
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2452 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 13:47 · PVG 21:47 · LAX 06:47 · JFK 09:47
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.