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
a379395979
V2EX  ›  iDev

tableView: UITableView, numberOfRowsInSection 调用不了?

  •  
  •   a379395979 · 2015-11-18 15:44:57 +08:00 · 5176 次点击
    这是一个创建于 3082 天前的主题,其中的信息可能已经有所发展或是发生改变。
    var weibos: [
    Int] =[];
    
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    print("条数\(weibos?.count)");
    return weibos?.count ?? 0
    
    }
    
    func loadData(){
            //WeiboModel.loadData(0, max_id: 0) { (list, error) -> () in
                self.weibos = [1];
                print(self.weibos);
                print(self.weibos?.count);
                self.tableView.reloadData();
            //}
    }
    

    以上 uitableview 的代理方法, app 启动的时候,能输出 “条数 1 ”
    但是 如果将 WeiboModel.loadData 注释去掉,
    函数里的代码还是能执行 print(self.weibos); 等于 1

    但是 func tableView(tableView: UITableView, numberOfRowsInSection section: Int) 这个代理方法就执行不了,导致页面表格出不来。。。

    甚至我延迟 10 秒执行 print(self.weibos?.count);self.tableView.reloadData();,表格还是出不来。数组里 weibos 是有值的。。

    为什么加了这个函数 WeiboModel.loadData 之后 self.tableView.reloadData()就不起作用了。里面只是封装了一个 AFNetworking 的请求而已。。。

    6 条回复    2015-11-22 23:58:31 +08:00
    jackisnotspirate
        1
    jackisnotspirate  
       2015-11-18 16:44:19 +08:00
    看先你的 weiboModel
    a379395979
        2
    a379395979  
    OP
       2015-11-18 16:56:32 +08:00
    @jackisnotspirate

    // WeiboModel 的一个 func
    class func loadData(since_id: Int, max_id: Int, finished: (list: [WeiboModel]?, error: NSError?)-> ()) {
    NetworkTool.shareNetworkTool.loadWeibo(since_id, max_id: max_id) { (result, error) -> () in
    // 判断是否有错误
    if error != nil {
    print("加载微博数据出错:\(error!)")
    finished(list: nil, error: error)
    }
    // 获取返回数据里的微博数据
    if let array = result?["statuses"] as? [[String : AnyObject]] {
    // 创建模型数组
    var list : [WeiboModel] = [];
    for dict in array {
    // 字典转模型并添加到模型数组中
    list.append(WeiboModel(dict: dict))
    }
    finished(list:list,error:nil);
    } else {
    // 没有加载到数据
    finished(list: nil, error: nil)
    }
    }
    }
    tane05
        3
    tane05  
       2015-11-18 17:49:24 +08:00 via iPhone
    条件反射 delegate 设了没?是主线程没?
    zepto
        4
    zepto  
       2015-11-18 19:27:29 +08:00
    self.tableView.reloadData() 理论上需要在主线程中进行
    a379395979
        5
    a379395979  
    OP
       2015-11-18 20:19:01 +08:00
    https://github.com/iScript/swift-project/blob/master/ySeed/HomeViewController.swift
    这是代码,哪位大神帮忙看看啊。。

    数组里是有值的, reloadData()就是不行。。 注释掉 WeiboModel.loadData(0, max_id: 0) { (list, error) -> () in , reloadData()就可以了。。 数组是同样的值。都写死了。
    sablib
        6
    sablib  
       2015-11-22 23:58:31 +08:00
    如果是注释掉 WeiboModel.loadData 就好了的话,很有可能是因为你的那个 loadData 的回调不是在主线程执行的。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3165 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 00:15 · PVG 08:15 · LAX 17:15 · JFK 20:15
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.