ios - Swift UITableView 真的很慢

标签 ios iphone uitableview swift2

我有一个简单的模型 E-Numbers,它从 .csv 文件中读取。我将数据缓存到我的 ViewController 中的变量 eNummers 中,这样我就可以执行基本的数组操作,例如 eNummers[0]eNummers[1].

我从 tableView:cellForRowAtIndexPath 中的数组中读取,但是当我滚动 tableView 时,它确实非常缓慢和滞后。我该如何优化它:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        var cell = tableView.dequeueReusableCellWithIdentifier("globalCell") as UITableViewCell!

        if cell == nil {
            cell = UITableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier: "globalCell")
        }

        if (self.eNummers?.getAllNummers().rows[indexPath.row] != nil) {
            cell.textLabel?.text = self.eNummers?.getAllNummers().rows[indexPath.row][0]
            cell.detailTextLabel?.text = self.eNummers?.getAllNummers().rows[indexPath.row][1]

            switch (self.eNummers?.getAllNummers().rows[indexPath.row][2])! {
                case "GOOD":
                    cell.textLabel?.textColor = UIColor.greenColor()
                break
                case "MODERATE":
                    cell.textLabel?.textColor = UIColor.orangeColor()
                break
                case "BAD":
                    cell.textLabel?.textColor = UIColor.redColor()
                break
                default:
                    cell.textLabel?.textColor = UIColor.blackColor()
                break
            }

        }

        return cell
    }

最佳答案

您的函数 getAllNummers() 正在加载一个 CSV 文件并为每个单元格解析 4 次。这将是你的大减速。您应该一次加载和解析文件并将值存储在一个属性中,然后使用该属性访问您需要的值

关于ios - Swift UITableView 真的很慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31625539/

相关文章:

ios - 使用 UIIImagePicker 的 CGAffineTransform 缩放 UIImage 并保存到解析 SDK

ios - iOS Team Provisioning Profile 是否与开发 aps 环境兼容?

iphone - Apple 的 Reachability 类是否有更复杂的替代品?

iphone - 如果可能的话,如何从网站添加指向 iPhone native 应用程序的链接

iphone - 如何使用 UITableView 在 iOS 中正确显示三层数据结构

ios - 为什么可访问性将 NSString 属性添加为静态文本?以及如何忽略它?

ios - 是否可以从 tableView :numberOfRowsInSection? 返回 NSUInteger

ios - 停止调用 -removeAllAnimaitions

ios - collectionview global header 不会将单元格向下推而是保持在顶部

ios - 如何使通用 Xcode 5 应用程序项目以横向启动?