我在那个单元格中有一个UITableviewCell
,我添加了name
、emp_id
和UIImageview
来显示我有 2 个 url 的数据,其中一个 url 显示名称和emp_id's
,另一个 URL 沿emp_id's
有图像(与emp_id相同(,我必须借助 emp_id's
将该图像显示给那里的名称。我能够显示详细信息,但无法实现图像,这是我的代码
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)
}
}
}