gpt4 book ai didi

ios - 在表格 View 中滚动时图像重复单元格

转载 作者:行者123 更新时间:2023-11-30 12:11:23 24 4
gpt4 key购买 nike

图像在表格 View 中无法正确显示,我有两所 Json Api(小学/高中)学校。我可以附加 Json api 数据并显示到 TableView 中,表格 View 工作正常,它显示了(小学/高中)学校数据。当我可以滚动桌面 View 时,图像会跳跃,并且图像在桌面 View 的 ImageView 中加载速度非常慢。

在滚动tableview之前它的显示是这样的

enter image description here

enter image description here

滚动桌面 View 后,它显示如下

滚动后图像会跳跃,

enter image description here

这是代码

     var kidsdata = [KidDetails]()

func getprimarydata(_firsturl: String,firstid:String,updatedate:String)
{


if errorCode == "0" {

if let kid_list = jsonData["students"] as? NSArray {

self.kidsdata.removeAll()

for i in 0 ..< kid_list.count {

if let kid = kid_list[i] as? NSDictionary {


let imageURL = url+"/images/" + String(describing: kid["photo"]!)

self.kidsdata.append(KidDetails(
name:kid["name"] as? String,
photo : (imageURL),
standard: ((kid["standard"] as? String)! + "std" + " " + (kid["section"] as? String)! + " section ")
))}}}}
}


func gethighdata(_secondurl:String ,secondid:String,updatedate:String)
{
if errorCode == "0" {

if let kid_list = jsonData["students"] as? NSArray {
for i in 0 ..< kid_list.count {


if let kid = kid_list[i] as? NSDictionary {

let imageURL = url+"/images/" + String(describing: kid["photo"]!)

self.kidsdata.append(KidDetails(
name:kid["name"] as? String,
photo : (imageURL),

standard: ((kid["standard"] as? String)! + "th" + " " + (kid["section"] as? String)! + " section ")
)
)
}
}


self.do_table_refresh()
}
}
}


func do_table_refresh()
{

DispatchQueue.main.async(execute: {

self.TableView.reloadData()

return
})

}


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

cell.ProfileImage?.image = nil

let row = (indexPath as NSIndexPath).row


let kid = kidsdata[row] as KidDetails

cell.NameLabel.text = kid.name

cell.ProfileImage.image = UIImage(named: "profile_pic")

cell.ProfileImage.downloadImageFrom(link:kid.photo!, contentMode: UIViewContentMode.scaleAspectFill)
cell.ClassNameLabel.text = kid.standard
return cell
}

我哪里做错了,请帮助我......!

最佳答案

AlamofireImage 很好地处理了这个问题。 https://github.com/Alamofire/AlamofireImage

import AlamofireImage

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

let kid = kidsdata[indexPath.row] as KidDetails


cell.NameLabel.text = kid.name
cell.ClassNameLabel.text = kid.standard

// assuming cell.ProfileImage is a UIImageView
cell.ProfileImage.image = nil

let frame = CGSize(width: 50, height: 50)
let filter = AspectScaledToFillSizeWithRoundedCornersFilter(size: frame, radius: 5.0)

cell.ProfileImage.af_setImage(withURL: urlToImage, placeholderImage: nil, filter: filter,
imageTransition: .crossDissolve(0.3), runImageTransitionIfCached: false)

return cell
}

关于ios - 在表格 View 中滚动时图像重复单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46008023/

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