[英]Prevent reloading on reusable cells in Swift
我是Swift語言的新手,現在遇到了問題...
在“我的個人資料”表視圖中,我使用指向包含另一個類的已標記對象的指針數組的includeKey來查詢(以Parse)所有currentUser()的帖子。
對於每個帖子,我必須為根據其“類型”標記的每個對象設置不同的顏色(使用AttributedStrings連接字符串)
我發現每次向下滾動時都在重新加載單元格,但問題是代碼變得非常繁瑣和緩慢。
您是否知道如何解決此問題或如何防止單元被重用?
非常感謝。
這是來自cellForRowAtIndexPath()的代碼:
cell.selectionStyle = UITableViewCellSelectionStyle.None
// pour activer la selection de cellules
// cell.selectionStyle = UITableViewCellSelectionStyleBlue;(By Default)
// cell.selectionStyle = UITableViewCellSelectionStyleGray;
var imageToLoad = self.images[indexPath.row] as PFFile
var imageCaption = self.imageCaptions[indexPath.row] as String
var imageDate = self.imageDates[indexPath.row] as String
var postKooleqts: AnyObject = self.kooleqts[indexPath.row] as AnyObject
imageToLoad.getDataInBackgroundWithBlock{(imageData, error) -> Void in
if (error == nil){
var finalizedImage = UIImage(data:imageData!)
cell.postImage.image = finalizedImage
}
}
cell.postCaption.text = imageCaption
cell.postDate.text = imageDate
cell.nbKooleqts.text = "\(postKooleqts)"
var x = 0
var htmlString = ""
if (linkedPages[indexPath.row].count > 0){
while x < linkedPages[indexPath.row].count {
println("indexx : \([indexPath.row])")
println("stringg : \(htmlString)")
let pageToAdd = linkedPages[indexPath.row][x]["name"] as! String
//let test = linkedPages[indexPath.row][x]
//println("resultat \([indexPath.row]) : \([test])")
if (linkedPages[indexPath.row][x]["type"] as! String == "obj"){
stringToAdd = "<span style=\"font-family : HelveticaNeue; font-size : 15; color: white;\"><span style=\"font-weight: bold; color: #c00b0b\">\(pageToAdd) </span>- -</span>"
}
else if (linkedPages[indexPath.row][x]["type"] as! String == "bra"){
stringToAdd = "<span style=\"font-family : HelveticaNeue; font-size : 15; color: white;\"><span style=\"font-weight: bold; color: #0099ff\">\(pageToAdd)</span>- -</span>"
}
else if (linkedPages[indexPath.row][x]["type"] as! String == "peo"){ let stringToAdd = "<span style=\"font-family : HelveticaNeue; font-size : 15; color: white;\"><span style=\"font-weight: bold; color: #0bdf6a\">\(pageToAdd)</span>- -</span>"
}
else if (linkedPages[indexPath.row][x]["type"] as! String == "eve"){
stringToAdd = "<span style=\"font-family : HelveticaNeue; font-size : 15; color: white;\"><span style=\"font-weight: bold; color: #cc99ff\">\(pageToAdd)</span>- -</span>"
}
else if (linkedPages[indexPath.row][x]["type"] as! String == "pla"){
stringToAdd = "<span style=\"font-family : HelveticaNeue; font-size : 15; color: white;\"><span style=\"font-weight: bold; color: #e16818\">\(pageToAdd)</span>- -</span>"
} else {stringToAdd = ""}
htmlString = htmlString + "\(stringToAdd)"
var encodedData = htmlString.dataUsingEncoding(NSUTF8StringEncoding)!
var attributedOptions = [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType]
let attributedString = NSAttributedString(data: encodedData, options: attributedOptions, documentAttributes: nil, error: nil)
cell.linkedPages.attributedText = attributedString
x++
}
}else {
println("empty")
cell.linkedPages.text = ""
}
return cell
}
更新 :鏈接的頁面似乎是主要的滯后問題,好像我刪除它,不再滯后
UPDATE2 :這是工作之前/之后的工作代碼,不再滯后:
var x = 0
var htmlString = ""
var attributedString = NSMutableAttributedString()
var attrs:[String : AnyObject]?
if (linkedPages[indexPath.row].count > 0){
while x < linkedPages[indexPath.row].count {
//println("indexx : \([indexPath.row])")
//println("stringg : \(htmlString)")
var pageToAdd = linkedPages[indexPath.row][x]["name"] as! String
attributedString = NSMutableAttributedString(attributedString: attributedString)
var addComma = NSMutableAttributedString(string: ", ")
var paraStyle = NSMutableParagraphStyle()
paraStyle.paragraphSpacing = 15.0
if (linkedPages[indexPath.row][x]["type"] as! String == "obj"){
attrs = [NSFontAttributeName : UIFont.boldSystemFontOfSize(15.0), NSForegroundColorAttributeName : UIColor(red: 192.0/255.0, green: 11.0/255.0, blue: 11.0/255.0, alpha: 1.0), NSParagraphStyleAttributeName : paraStyle]
}
else if (linkedPages[indexPath.row][x]["type"] as! String == "bra"){
attrs = [NSFontAttributeName : UIFont.boldSystemFontOfSize(15.0), NSForegroundColorAttributeName : UIColor(red: 12.0/255.0, green: 153.0/255.0, blue: 255.0/255.0, alpha: 1.0), NSParagraphStyleAttributeName : paraStyle]
}
else if (linkedPages[indexPath.row][x]["type"] as! String == "peo"){
attrs = [NSFontAttributeName : UIFont.boldSystemFontOfSize(15.0), NSForegroundColorAttributeName : UIColor(red: 11.0/255.0, green: 223.0/255.0, blue: 106.0/255.0, alpha: 1.0), NSParagraphStyleAttributeName : paraStyle]
}
else if (linkedPages[indexPath.row][x]["type"] as! String == "eve"){
attrs = [NSFontAttributeName : UIFont.boldSystemFontOfSize(15.0), NSForegroundColorAttributeName : UIColor(red: 204.0/255.0, green: 153.0/255.0, blue: 255.0/255.0, alpha: 1.0), NSParagraphStyleAttributeName : paraStyle]
}
else if (linkedPages[indexPath.row][x]["type"] as! String == "pla"){
attrs = [NSFontAttributeName : UIFont.boldSystemFontOfSize(15.0), NSForegroundColorAttributeName : UIColor(red: 225.0/255.0, green: 90.0/255.0, blue: 1.0/255.0, alpha: 1.0), NSParagraphStyleAttributeName : paraStyle]
}
var coloredString = NSMutableAttributedString(string:"\(pageToAdd)", attributes:attrs)
attributedString.appendAttributedString(coloredString)
attributedString.appendAttributedString(addComa)
cell.linkedPages.attributedText = attributedString
x++
}
}else {
println("empty")
cell.linkedPages.text = ""
}
return cell
可能的原因是您為每個單元格中的圖像調用了getData()
。 據我所知,沒有辦法不重復使用單元格,因為這是該方法設計的目的。 之所以變得如此緩慢,是因為OS試圖等待加載每個圖像才能顯示該單元,並且這樣做會使它很快停頓下來。 相反,您需要做的是為單元格設置默認圖像,然后調用var imageData = imageToLoad.getDataInBackground({})
。 這將允許在滾動時返回單元格,然后在圖像加載后填充圖像,而不是等待繪制單元格直至檢索到圖像。
為什么不實現圖像緩存,而不是每次創建單元時都不下載,而是檢查並查看是否還沒有下載圖像,以及是否使用了以前下載的副本。
您將必須根據內存需求調整緩存大小,並彈出與表中當前位置相距甚遠的圖像。
您的代碼還存在另一個問題,如果您快速向下滾動,則會排隊等待大量下載。 理想情況下,您需要有一種方法可以取消不再顯示的單元格的下載,或者停止后台下載,直到表格停止滾動為止。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.