【问题标题】:Faster way to populate a UITableView?填充 UITableView 的更快方法?
【发布时间】:2015-05-08 09:30:55
【问题描述】:

我的表格视图中有 4 个部分,每个部分包含 2500 个单元格。加载大约需要 9 秒。我想异步填充 4 个部分,这可能吗?

编辑:现在填充表格视图的代码。

//Contains 2500 names each.
let namesForSection0:[String] = []
let namesForSection1:[String] = []
let namesForSection2:[String] = []
let namesForSection3:[String] = []

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{

    let cell: UITableViewCell = self.tableview.dequeueReusableCellWithIdentifier("catcell") as! UITableViewCell

    switch indexPath.section {

    case 1:
        cell.textLabel?.text = namesForSection0[indexPath.row]

    case 2:
        cell.textLabel?.text = namesForSection1[indexPath.row]

    case 3:
        cell.textLabel?.text = namesForSection2[indexPath.row]

    case 4:
        cell.textLabel?.text = namesForSection3[indexPath.row]

    default:

        println("something is wrong")

    }

}

【问题讨论】:

  • 显示数据是什么以及您当前的操作方式。你有没有分析过什么是慢的?
  • @Wain 代码已发布。
  • 你是如何计算单元格高度的?
  • @dan 除了使用 heightForRowAtIndexPath,所有单元格的高度都是 44。
  • 那么哪个部分需要 4 秒?字符串是从哪里来的?

标签: ios uitableview swift asynchronous populate


【解决方案1】:

解析 JSON 时,首先为每个数组加载前 50 个左右的成员。不要一次解析所有内容。数组需要是可变的,即var namesForSection0:[String] = [],而不是let。重新加载表格视图。

然后解析完整的 JSON 并将新成员添加到数组中。确保不要添加重复项(这是特定于您的数据的)。加载它们后,在您的 tableview 上调用 reloadData。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-02-20
    • 1970-01-01
    • 2021-07-19
    • 1970-01-01
    • 1970-01-01
    • 2014-09-28
    • 1970-01-01
    相关资源
    最近更新 更多