我是 Swift 语言的新手,我遇到了一个问题...
在“我的个人资料”表格 View 中,我使用指向其他类的标记对象的指针数组的 includeKey 查询(解析)所有 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
}
更新: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
最佳答案
可能的原因是您为每个单元格中的图像调用了 getData()
。据我所知,没有办法不重用单元格,因为这正是该方法设计的目的。这变得如此缓慢的原因是操作系统试图等待加载每个图像以显示单元格,并且这样做会很快陷入困境。相反,您需要做的是为单元格设置默认图像,然后调用 var imageData = imageToLoad.getDataInBackground({})
。这将允许在滚动时返回单元格,然后图像将在加载后填充,而不是等待绘制单元格直到检索到图像。
关于ios - 防止在 Swift 中重新加载可重用单元,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31549210/