防止重新加载 Swift 中的可重用单元格
Prevent reloading on reusable cells in Swift
我是 Swift 语言的新手,我遇到了一个问题...
在 "My profile" 表视图中,我查询(解析)所有 currentUser() 的 posts,其中包含指向来自另一个 Class.
对于每个 post,我必须为根据其 "type" 标记的每个对象设置不同的颜色(使用 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
}
UPDATE : linkedPages 似乎是主要的滞后问题,就像我删除它一样,它不再滞后
UPDATE2:这是具有 before/after 的工作代码,不再有滞后:
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({})
。这将允许在滚动时返回单元格,然后图像将在加载后填充,而不是等待绘制单元格直到检索到图像。
你为什么不实现一个图像缓存,而不是每次创建单元格时都下载,检查你是否还没有下载图像,以及是否使用了以前下载的副本。
您必须根据内存要求调整缓存大小,并弹出远离当前位置的图像 table。
您的代码还有另一个问题,如果您非常快地向下滚动,它将排队等待大量下载。理想情况下,您需要有一种方法可以取消不再显示的单元格的下载,或者停止后台下载直到 table 停止滚动。
我是 Swift 语言的新手,我遇到了一个问题...
在 "My profile" 表视图中,我查询(解析)所有 currentUser() 的 posts,其中包含指向来自另一个 Class.
对于每个 post,我必须为根据其 "type" 标记的每个对象设置不同的颜色(使用 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
}
UPDATE : linkedPages 似乎是主要的滞后问题,就像我删除它一样,它不再滞后
UPDATE2:这是具有 before/after 的工作代码,不再有滞后:
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({})
。这将允许在滚动时返回单元格,然后图像将在加载后填充,而不是等待绘制单元格直到检索到图像。
你为什么不实现一个图像缓存,而不是每次创建单元格时都下载,检查你是否还没有下载图像,以及是否使用了以前下载的副本。
您必须根据内存要求调整缓存大小,并弹出远离当前位置的图像 table。
您的代码还有另一个问题,如果您非常快地向下滚动,它将排队等待大量下载。理想情况下,您需要有一种方法可以取消不再显示的单元格的下载,或者停止后台下载直到 table 停止滚动。