我有一个简单的模型 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/