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

遇到一个奇怪的问题, TableView 需要触摸一下才显示出来,否则等 10 秒以上自动显示。

  •  
  •   babyname · 2015-04-10 22:59:04 +08:00 · 2852 次点击
    这是一个创建于 3309 天前的主题,其中的信息可能已经有所发展或是发生改变。
    我通过网络加载数据才出现这种情况。

    通过 debug 发现,启动后请求远程数据,然后执行了 tableView.reload ,调用 tableView(tableView: UITableView, numberOfRowsInSection section: Int) 可以返回正确的 rows 但是后续却没有执行 tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell 方法,如果此时触摸(随便滚动一下)就会执行该方法,显示数据。否则,等待大概10秒,才会自动显示数据。

    有遇到过的吗?可能是什么原因?
    7 条回复    2015-04-11 09:36:55 +08:00
    tane05
        1
    tane05  
       2015-04-11 00:04:05 +08:00
    这个有点玄学, 掉到numberOfRowsInSection说明tableView在布局计算了.
    确定numberOfRowsInSection 返回值大于0?
    确定没其他因素干扰tableView的contentsize属性?
    babyname
        2
    babyname  
    OP
       2015-04-11 00:30:51 +08:00
    @tane05 搞好了,线程问题,异步加载的数据,然后回调方法中 reloadData 所以不行,需要手动拖动 TableView 使其重汇 UI,通过

    func didRecevedResults() {
    // some...
    dispatch_async(dispatch_get_main_queue(), {
    self.tableView.reloadData()
    }
    }
    就好了
    kshatriya
        3
    kshatriya  
       2015-04-11 04:04:34 +08:00
    记得还要+ (BOOL)requiresConstraintBasedLayout
    {
    return YES;
    }
    Elethom
        4
    Elethom  
       2015-04-11 08:19:34 +08:00 via iPhone
    看標題就知道是沒在主線程執行,接觸 iOS 應用程式開發一週內的新手偶爾會犯的錯誤。看一下 UIKit 文檔吧,少年。
    babyname
        5
    babyname  
    OP
       2015-04-11 08:41:16 +08:00 via iPhone
    @Elethom 谢谢,文档没怎么看,麻烦解释下为什么不在主线程执行也会显示 只是会延迟?
    Elethom
        6
    Elethom  
       2015-04-11 09:07:36 +08:00
    likid
        7
    likid  
       2015-04-11 09:36:55 +08:00
    All UI must be updated on main threadhttp://stackoverflow.com/questions/24895175/ios-tableview-reloaddata
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2148 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 02:02 · PVG 10:02 · LAX 19:02 · JFK 22:02
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.