gpt4 book ai didi

ios - 如何在 swift 中将图像 url (动态)分配给特定 emp id 的 ImageView

转载 作者:行者123 更新时间:2023-11-29 05:35:48 26 4
gpt4 key购买 nike

我在该单元格中有一个 UITableviewCell,我添加了 nameemp_idUIImageview 来显示数据我有2个网址,其中一个网址显示名称和emp_id's,另一个网址沿emp_id's显示图像(相同的emp_id's),我必须使用emp_id 的帮助。我能够显示详细信息,但无法实现图像,这是我的代码

struct jsonstruct5:Decodable {
var name:String
var emp_id:String
var url:String?
}

struct jsonstruct21:Decodable {
var url:String?
var emp_id:String
}

var arrdata = [jsonstruct5]()
var arrdata1 = [jsonstruct21]()



func getdata(){
let url = URL(string: "https://sp/company/employees_detail/app")
URLSession.shared.dataTask(with: url!) { (data, response, error) in
do{if error == nil{
self.arrdata = try JSONDecoder().decode([jsonstruct5].self, from: data!)

for mainarr in self.arrdata{
// print(data)

DispatchQueue.main.async {
self.tableView.reloadData()
}
}
}

}catch{
print("Error in get json data")
}

}.resume()
}

响应是

[  
{
"name":"Sonu",
"emp_id":"01"
},
{
"name":"Prasanth",
"emp_id":"02"
},
{
"name":"Patra",
"emp_id":"03"
}.
]
func getdata1(){
let url = URL(string: "https://sp/company/employees_detail/profile/photos")
URLSession.shared.dataTask(with: url!) { (data, response, error) in
do{if error == nil{
self.arrdata1 = try JSONDecoder().decode([jsonstruct21].self, from: data!)

for mainarr1 in self.arrdata1{
// print(mainarr.name,":",mainarr.dob)
print(data)
print(mainarr1.url)
let data1 = try? Data(contentsOf: url!)
print(data1)
if let imageData = data {
let image4 = UIImage(data: imageData)
}
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
}

}catch{
print("Error in get json data")
print(error)
}

}.resume()
}

响应是

[  
{
"url":"https//ps/Image2",
"emp_id":"01"
},
{
"url":null,
"emp_id":"02"
},
{
"url":"https//ps/Image4",
"emp_id":"03"
}
]
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

let cell:AppreTableViewCell = tableView.dequeueReusableCell(withIdentifier: "cell") as! AppreTableViewCell
cell.nameLbl.text = "\(arrdata[indexPath.row].name)"
cell.dateLbl.text = "\(arrdata[indexPath.row].emp_id)"
print(DataManager.sharedInstance.empID)

if (arrdata[indexPath.row].emp_id == DataManager.sharedInstance.empID)
{
cell.isHidden=true
}
else{
cell.isHidden=false
}
// tableView.alwaysBounceVertical = false
return cell
}

最佳答案

尝试下面

struct jsonstruct5:Decodable {
var name:String
var emp_id:String
var url:String?
}

struct jsonstruct21:Decodable {
var url:String?
var emp_id:String
}

var arrdata = [jsonstruct5]()
var arrdata1 = [jsonstruct21]()

func getdata(){
let url = URL(string: "https://sp/company/employees_detail/app")
URLSession.shared.dataTask(with: url!) { (data, response, error) in

do { if error == nil {
self.arrdata = try JSONDecoder().decode([jsonstruct5].self, from: data!)
self. getdata1()
// don't for put reloadData in for loops, always play with your data/model and after loop you can call reloadData method.

}catch{
print("Error in get json data")
}

}.resume()
}



func getdata1(){
let url = URL(string: "https://sp/company/employees_detail/profile/photos")
URLSession.shared.dataTask(with: url!) { (data, response, error) in
do{if error == nil{
self.arrdata1 = try JSONDecoder().decode([jsonstruct21].self, from: data!)

// I don't know your model type, but you can put "url" from each iteration of second array (arrdata1) into first array (arrdata) where employee id matches.
// don't download images in below loop, rather download them in cellForRowAt method.
// then you just call self.tableView.reloadData()

// update your model
for item1 in arrdata1 {
if let index = arrdata.index(where: {$0["emp_id"] == item1["emp_id"]}) {
var item = arrdata[index]
item["url"] = item1["url"]
arrdata[index] = item
}
}

// or below code
for item1 in arrdata1 {
if let index = arrdata.index(where: {$0.emp_id == item1.emp_id}) {
var item = arrdata[index]
item.url = item1.url
arrdata[index] = item
}
}


// model array has been updated, now work on cellForRowAt method

DispatchQueue.main.async {
self.tableView.reloadData()
}
}

}catch{
print("Error in get json data")
print(error)
}

}.resume()
}




func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

let cell:AppreTableViewCell = tableView.dequeueReusableCell(withIdentifier: "cell") as! AppreTableViewCell
let model = arrdata[indexPath.row]
cell.nameLbl.text = "\(model.name)"
cell.dateLbl.text = "\(model.emp_id)"

// here you download image for only current model.url
if let url = model.url {
cell.nameOfImageView.loadImageUsingCacheWithURLString(url, placeHolder: UIImage(named: "someImage")!, completionBlock: { (image) in
cell.nameOfImageView.image = image
})
}

// tableView.alwaysBounceVertical = false
return cell
}


let imageCache = NSCache<NSString, UIImage>()

extension UIImageView {
func loadImageUsingCacheWithURLString(_ URLString: String, placeHolder: UIImage, completionBlock:@escaping (_ image:UIImage)->()) {
if let cachedImage = imageCache.object(forKey: NSString(string: URLString)) {
self.image = cachedImage
completionBlock(cachedImage)
}

else if let url = URL(string: URLString) {
URLSession.shared.dataTask(with: url, completionHandler: { (data, response, error) in

//print("RESPONSE FROM API: \(response)")
if error != nil {
print("ERROR LOADING IMAGES FROM URL: \(error!.localizedDescription)")
DispatchQueue.main.async {
self.image = placeHolder
completionBlock(placeHolder)
}
}
else {
DispatchQueue.main.async {
if let data = data {
if let downloadedImage = UIImage(data: data) {
imageCache.setObject(downloadedImage, forKey: NSString(string: URLString))
self.image = downloadedImage
completionBlock(downloadedImage)
}
else {
self.image = placeHolder
completionBlock(placeHolder)
}
}
else {
self.image = placeHolder
completionBlock(placeHolder)
}
}
}
}).resume()
}
else if URLString.isEmpty {
completionBlock(placeHolder)
}
}
}

关于ios - 如何在 swift 中将图像 url (动态)分配给特定 emp id 的 ImageView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57053175/

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