2016-12-25 1 views
1

私は素早く勉強しており、最終的に画像を非同期で読み込む方法を発見しました。私が今しようとしているのは、そのコードを私のTableViewに渡すことですが、私のコードファイルがどのように構造化されているかを見て、それをどうやって行うのか少し困惑しています。ここでは最初に、単一のイメージを示すコードを示します。私のコードフォーマットで私のTableViewで動的画像を読み込むにはどうすればいいですか?

login.swift:私はTableViewCellや他のAのための2つのファイル1を持っている:

let imgURL: NSURL = NSURL(string: "my-url")! 
let request:NSURLRequest = NSURLRequest(url: imgURL as URL) 
let config = URLSessionConfiguration.default 
let session = URLSession(configuration: config) 

let task = session.dataTask(with: request as URLRequest, completionHandler: {(data, response, error) in 
    DispatchQueue.main.async(execute: {() -> Void in 
     self.Profile_Image.image = UIImage(data: data!) 
    }) 
}); 

task.resume() 

上記のこのコードを正しくは今、私はそのコードを渡し、テーブルビューに挿入したい画像をレンダリングTableViewを持つコントローラ。

TableViewCell.swift:

class TableViewCell: UITableViewCell { 

    @IBOutlet weak var fullname: UIButton! 
    @IBOutlet weak var profile_image: UIImageView! 


    override func awakeFromNib() { 
     super.awakeFromNib() 

    } 

    override func setSelected(_ selected: Bool, animated: Bool) { 
     super.setSelected(selected, animated: animated) 

    } 

} 

Controller.swift:

class Controller: UIViewController,UITableViewDataSource { 
    var FullName = [String]() 
    var profile_image_string = [String]() 
    @IBOutlet weak var TableSource: UITableView! 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     TableSource.dataSource = self 
     getJsonData() 
    } 

    func getJsonData() { 

    URLSession.shared.dataTask(with:request, completionHandler: {(data, response, error) in 
      if error != nil { 

      } else { 
       do { 
        let parsedData = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as! [String:Any] 

        if let Streams = parsedData["JsonData"] as? [AnyObject] { 

         for Stream in Streams { 
          if let fullname = Stream["fullname"] as? String { 
           self.FullName.append(fullname) 
          } 

          if let profile_picture = Stream["profile_image"] as? String { 
           self.profile_image_string.append(profile_picture) 
          } 

         } 

         DispatchQueue.main.async { 
          self.TableSource.reloadData() 

         } 
        } 
       } catch let error as NSError { 
        print(error) 
       } 
      } 

     }).resume() 
} 



    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "TVC", for: indexPath) as! TableViewCell 

     cell.fullname.setTitle(FullName[indexPath.row],for: UIControlState.normal) 
     cell.profile_image.tag = indexPath.row 

     return cell 
    } 

} 

[OK]を私は基本的に保存し、可変するvar profile_image_stringを持って私のコントローラクラスの最上部にあるように、 Jsonから来るすべての異なるURLを次にこのコードを使用して追加します

if let profile_picture = Stream["profile_image"] as? String { 
           self.profile_image_string.append(profile_picture) 
          } 

今、私はImageViewプロパティにTableViewコード内でアクセスできます。画像を表示するにはどうすればいいですか?あなたは私が...この

cell.profile_image. /// 

任意の提案は素晴らしいことだかを実行してImageViewのアクセステーブルビュー(UITableViewCellの)内のコードを見ることができれば

+0

このライブラリを使用する:https://github.com/onevcat/Kingfisher非常に使いやすく、cellForRowAtIndexで使用する:let url = URL(文字列: "url_of_your_image") imageView.kf.setImage(with:url ) –

答えて

1

あなたは、以下のようなあなたのcellForRowAtメソッドを定義することができます。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "TVC", for: indexPath) as! TableViewCell 

     cell.fullname.setTitle(FullName[indexPath.row],for: UIControlState.normal) 
     cell.profile_image.tag = indexPath.row 

     /* Set your cell image here*/ 
      let strCellImageURL = yourArray.objectAtIndex(indexPath.row) 

      let imgURL: NSURL = NSURL(string: strCellImageURL)! 
      let request:NSURLRequest = NSURLRequest(url: imgURL as URL) 
      let config = URLSessionConfiguration.default 
      let session = URLSession(configuration: config) 

      let task = session.dataTask(with: request as URLRequest, completionHandler: {(data, response, error) in 
      DispatchQueue.main.async(execute: {() -> Void in 
      cell.Profile_Image.image = UIImage(data: data!) 
     }) 
    }); 

     return cell 
    } 

Nativeで何か良いものが用意されていれば、どうして私たちは第三者のクラスに行くべきなのでしょうか?

説明が必要な場合は、下記にコメントしてください。

+0

完璧なおかげで多くのことが働いた –

+0

あなたの最もwelocome :) –

関連する問題