数据从核心数据加载到表格非常缓慢

时间:2015-06-25 05:18:51

标签: ios core-data

我在核心数据表中有100行,在tableView中我加载了25行。当我在应用程序中打开这个UIViewController它需要将近2秒,我认为这是非常缓慢的。我在iPhone 5上测试我的应用程序。可能是我做错了什么? 我看到cellForRowAtIndexPath方法为所有行调用4次:1-25,然后再次1-25等等。它可以吗? 当我只加载一行时,它的工作速度很快。

self.observers

2 个答案:

答案 0 :(得分:0)

您的estimatedHeightForRowAtIndexPath有一个问题。你在那里调用cellForRowAtIndexPath,这就是为什么你再次调用这个方法。您可以删除该代码并执行以下操作:

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
{
    return self.heightForBasicCellAtIndexPath(indexPath)
}

func heightForBasicCellAtIndexPath( indexPath: NSIndexPath) -> CGFloat
{
    var sizingCell: UITableViewCell? = nil;
    var token: dispatch_once_t = 0
    dispatch_once(&token) {
        sizingCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as? TipsTableViewCell
    }
    return sizingCell!.frame.size.height;
}

此方法使用GCD实例化sizingCell,以确保仅创建一次。

希望这有帮助

答案 1 :(得分:0)

我删除了heightForRowAtIndexPath和estimatedHeightForRowAtIndexPath并添加到viewDidLoad()这个:

tableView.rowHeight = UITableViewAutomaticDimension
 tableView.estimatedRowHeight = 160.0
相关问题