2016-09-01 5 views
1

私はSwiftを初めて使用しています。私は私のテーブルビューで画像を表示しようとしています。私はこれに従いますtutorial複数セクションのUITableViewでキャッシュタグ値を設定する方法をスウィフト

私のUITableViewでは、このtutorialに示されているように。私はキャッシュに画像を保存しています。 tableviewにセクションが1つしかない場合、これはうまく動作します。テーブルビューに複数のセクションがある場合、どのようにしてforKeyの値を設定する必要がありますか?
セクションと行に従ってこの値を設定するにはself.cache.setObject(image!, forKey:indexPath.row)?ありがとう!!

これは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 to 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 the image from 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 
     } 
    } 
+0

'tag'使用してみてください - あなたは – Rahul

+0

@Rahulの(アプリケーションでビューオブジェクトを識別するために使用できる整数。)キャッシュのキーとしてURL文字列を使用していませんか?同じイメージが複数の行、複数のセクション、または行の数が変更された場合にキャッシュが有効になります。 –

+1

なぜwouldnあなたのコメントをしてください手の込んだしてください可能性があり申し訳ありませんが代わりに' indexPath.row' – Paulw11

答えて

0

tagも使用できます。ここでは、タグ値を使用するコードです。また、コードのエラー処理を考慮する必要があるかもしれません。

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 and set it to cache objectForKey 
     // here I have taken big numbers so same number will not repeat 
     let tagValue = (indexPath.section * 100) + indexPath.row + 200 

     if (self.cache.objectForKey(tagValue) != nil){ 
      print("Cached image used, no need to 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 the image from 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 is nil") 
        } 
       }) 
       task.resume() 
      } 
      else { 
       print("Error : Image URL is nil") 
      } 

     } 
     cell = customCell 
    } 
    return cell 
} 
0

ための私のコードで問題がrow参照を使用してすべての画像をキャッシュしているということですが、あなたのUITableViewは、複数のセクションを持っている場合には、行の値を繰り返しています。例えば:

セクション0>、行0、列1、行2 セクション1>行0、列1 第2>行0

すべてのセクションでは、 "行0" を有します。

1)は、例えば、 "SECTION-ROW" のようなフォーマットされたキーを、作成します:

は、あなたがしなければならない、それを修正するには

self.cache.setObject(image!, forKey:"\(indexPath.section)-\(indexPath.row"))) 
self.cache.objectForKey(indexPath.row) 

2)と同じようLIBS使用:https://github.com/Haneke/HanekeSwift

1

をこのチュートリアルの著者は、キャッシュのキーとしてindexPath.rowを使用することに間違いを犯したようです。行番号は画像へのリンクが非常に乏しい。イメージを識別するものはそのURLです。行を使用することにより、問題があるでしょう:

  • 行番号の変更が(間違ってキャッシュヒット)
  • 複数のセクションに同じ画像がで使用されている
  • (あなたが発見したとして、単に一般的ickyness)があります。複数の行(不要なキャッシュミス)。

あなたは簡単にキャッシュキーとしてURLを使用するようcellForRowAtIndexPathを変更することができます。

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell 
{ 
    var cell = UITableViewCell() 
    if(tableView == tableview) { 
     let customCell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! CustomEventCell 
     customCell.cellBackgroundImg.image = UIImage(named: "placeholder") 

     if let img_url = myList[indexPath.section].objectAtIndex(indexPath.row).objectForKey("image") as? String { 

      if let image = self.cache.objectForKey(img_url) as? UIImage { 
       print("Cached image used, no need to download it") 
       cell.cellBackgroundImg?.image = image 
      } else { 
       print("Load the image from URL") 
       if 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 

         guard let downloadData= data && error == nil else { 
          print("error downloading image:\(error)" 
          return 
         } 
         dispatch_async(dispatch_get_main_queue(), {() -> Void in 
          if let image = UIImage(data: data) { 
           customCell.cellBackgroundImg?.image = image 
           self.cache.setObject(image, forKey:img_url) 
          } 
          }) 
        }) 
        task.resume() 
       } 
      } 
     } 
     cell = customCell 
    } 
    return cell 
} 

あなたのコードも非常に防御的ではなかったです。データが正しくない場合には例外を発生させる多くの強制アンラッピングがありました

関連する問題