我是新雨燕的語言和我面臨的一個問題...防止重裝上可重複使用的細胞斯威夫特
在「我的資料」的tableview,我查詢(解析)的所有currentUser()的帶有指向數組的指針數組的指針,指向來自其他類的標記對象。
對於每個崗位,我必須設置爲根據其「類型」標記的每個對象不同的顏色(使用AttributedStrings到連接的字符串)
我發現細胞每個I向下滾動但時間重裝事情是代碼變得相當沉重和緩慢。
你知道我該如何解決這個問題或者我可以如何防止細胞被重複使用?
非常感謝。
這是從的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
}
UPDATE:本linkedPages似乎是主要的滯後問題,因爲如果我刪除它,它不再滯後
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
也許我可以做的不是將linkedPages存儲爲指針數組,而是直接將AttributedString作爲HTML存儲。它應該執行得更快,但我不知道如何處理賽格。我會做一些測試,並保持更新 –