ios - Swift how to set cache tag value in UITableView for multiple sections -


i new swift. trying show images in tableview. following tutorial.

in uitableview, shown in tutorial. storing images in cache. works fine if tableview has 1 section. how should set forkey value if have multiple sections in tableview?
how set value self.cache.setobject(image!, forkey:indexpath.row) according section , row? thanks!!

this code uitableview

func numberofsectionsintableview(tableview: uitableview) -> int {     if(tableview == tableview){         return categoryarray.count     }else{         return 1     } }  func tableview(tableview : uitableview,  titleforheaderinsection section: int)->string {     if(tableview == tableview){         return categoryarray.objectatindex(section) as! string     }else{         return ""     } }  func tableview(tableview: uitableview, numberofrowsinsection section: int) -> int {     if(tableview == tableview){         print("count = \(mylist[section].count)")         return mylist[section].count     }     else{         return 5     }  }  func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell {     if(tableview == tableview) {         let cell = tableview.dequeuereusablecellwithidentifier("cell", forindexpath: indexpath) as! customeventcell  cell.cellbackgroundimg.image = uiimage(named: "placeholder")           if (self.cache.objectforkey(indexpath.row) != nil){             print("cached image used, no need download it")             cell.cellbackgroundimg?.image = self.cache.objectforkey(indexpath.row) as? uiimage         }else{              let img_url = mylist[indexpath.section].objectatindex(indexpath.row).objectforkey("image") as? string              if img_url != nil{                 print("load image url")                 let url = nsurl(string: img_url!)!                 let request = nsmutableurlrequest(url:url)                 let defaultsession = nsurlsession(configuration: nsurlsessionconfiguration.defaultsessionconfiguration())                 let task = defaultsession.datataskwithrequest(request, completionhandler: {(data:nsdata?, response:nsurlresponse?, error: nserror?) in          dispatch_async(dispatch_get_main_queue(), { () -> void in                          let image = uiimage(data: data!)                         cell.cellbackgroundimg?.image = image                         self.cache.setobject(image!, forkey:indexpath.row)                     })                 })                 task.resume()              }         }         return cell       }        } 

you can use tag also. here code uses tag value. may need consider error handling in code.

func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell {     var cell = uitableviewcell()     if(tableview == tableview) {         let customcell = tableview.dequeuereusablecellwithidentifier("cell", forindexpath: indexpath) as! customeventcell        customcell.eventtitle.text = title         customcell.cellbackgroundimg.image = uiimage(named: "placeholder")         // calculate tagvalue , set cache objectforkey        // here have taken big numbers same number not repeat          let tagvalue = (indexpath.section * 100) + indexpath.row + 200          if (self.cache.objectforkey(tagvalue) != nil){             print("cached image used, no need download it")             customcell.cellbackgroundimg?.image = self.cache.objectforkey(tagvalue) as? uiimage         }else{              let img_url = mylist[indexpath.section].objectatindex(indexpath.row).objectforkey("image") as? string              if img_url != nil{                 print("load image url")                 let url = nsurl(string: img_url!)!                 let request = nsmutableurlrequest(url:url)                 let defaultsession = nsurlsession(configuration: nsurlsessionconfiguration.defaultsessionconfiguration())                 let task = defaultsession.datataskwithrequest(request, completionhandler: {(data:nsdata?, response:nsurlresponse?, error: nserror?) in                      if error != nil {                         print(error)                         return                     }else if(data != nil){                         dispatch_async(dispatch_get_main_queue(), { () -> void in                             let image = uiimage(data: data!)                             customcell.cellbackgroundimg?.image = image                             self.cache.setobject(image!, forkey:tagvalue)                         })                     }else {                         print("error : image data nil")                     }                 })                 task.resume()             }             else {                 print("error : image url nil")             }          }        cell = customcell     }     return cell } 

Comments