2015-07-21 31 views
0

我是新雨燕的語言和我面臨的一個問題...防止重裝上可重複使用的細胞斯威夫特

在「我的資料」的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 
+0

也許我可以做的不是將linkedPages存儲爲指針數組,而是直接將AttributedString作爲HTML存儲。它應該執行得更快,但我不知道如何處理賽格。我會做一些測試,並保持更新 –

回答

1

可能的原因是您致電getData()查看每個單元格中的圖像。據我所知,沒有辦法不重用細胞,因爲這是方法設計要做的事。這個變得越來越慢的原因是操作系統試圖等待每個圖像被加載以顯示單元格,並且這樣做會使其非常快速地停止。相反,您需要爲單元設置默認圖像,然後致電var imageData = imageToLoad.getDataInBackground({})。這將允許單元格在滾動時返回,然後圖像將在它們加載後被填充,而不是等待繪製單元格直到圖像被檢索。

+0

我更新了我的代碼。我沒有主線程來調試控制檯中的消息了,但它仍然是滯後的 –

0

爲什麼不實施圖像緩存,而不是每次創建單元格時都下載,檢查並查看是否尚未下載圖像,以及是否使用了先前下載的副本。

您必須調整緩存的大小以滿足您的內存要求,並彈出遠離表格當前位置的圖像。

你的代碼還有另一個問題,如果你向下滾動真的很快,它會排隊很多很多的下載。理想情況下,您需要有一種方法來取消不再可見的單元格的下載,或者停止後臺下載,直到表格停止滾動。

+0

感謝這個想法,我打算在將來製作一個圖像緩存系統。但它不能解決我的「滯後」問題 –