ios - Prevent reloading on reusable cells in Swift -


i new swift language , facing problem...

in "my profile" tableview, query (to parse) currentuser()'s posts includekey of array of pointers point tagged objects other class.

for each post, have set different colors each object tagged according "type" (using attributedstrings concatenated strings)

i discovered cells reloading each time scroll down thing code becoming quite heavy , slow.

do have idea how fix or how prevent cells being reused ?

thanks lot.

this code 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] pffile     var imagecaption = self.imagecaptions[indexpath.row] string     var imagedate = self.imagedates[indexpath.row] string     var postkooleqts: anyobject = self.kooleqts[indexpath.row] 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 seems main lag issue if remove it, no longer lags

update2 : here working code have before/after, no lags anymore :

 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 

the reason call getdata() images in each cell. far know there no way not reuse cells, that's exaclty method designed do. reason getting slow os trying wait each image loaded in order display cell, , doing bog down quickly. instead need set default image cell, , call var imagedata = imagetoload.getdatainbackground({}). allow cells returned while scrolling, , images populated after they've loaded instead of waiting draw cell until image retrieved.


Comments

Popular posts from this blog

python - Healpy: From Data to Healpix map -

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -