gpt4 book ai didi

ios - 给定我的代码格式,如何在我的 TableView 中加载动态图像?

转载 作者:行者123 更新时间:2023-11-28 06:27:51 25 4
gpt4 key购买 nike

我正在学习 swift,我终于找到了如何异步加载图像。我现在想做的是将该代码传递到我的 TableView,但是考虑到我的代码文件的结构,我对如何做到这一点有点困惑。首先是显示单个图像的代码。

登录.swift:

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 中:我有 2 个文件,一个用于 TableViewCell,另一个用于带有 TableView 的 Controller 。

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
}

}

好的,所以在我的 Controller 类的顶部,我有变量 var profile_image_string,它基本上保存了来自 Json 的所有不同 URL,然后我使用此代码附加它

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

现在我只能访问 TableView 代码中的 ImageView 属性,我该如何显示我的图像?如果您可以看到 TableView(UITableViewCell) 中的代码,我会通过这样做来访问 imageView

cell.profile_image. ///

任何建议都会很棒......

最佳答案

您可以像下面这样定义您的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 中已经有一些好的东西,那么我们为什么要使用第三方类。

如果您需要任何说明,可以在下方评论我。

关于ios - 给定我的代码格式,如何在我的 TableView 中加载动态图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41318534/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com