[英]Swift UITableView really slow
我有一個簡單的模型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次。 這將是您的大減速。 您應該一次加載並解析文件,然后將值存儲在屬性中,然后使用該屬性訪問所需的值
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.