gpt4 book ai didi

ios - 快速返回另一页后, TableView 单元格正在复制

转载 作者:行者123 更新时间:2023-11-28 08:07:31 25 4
gpt4 key购买 nike

    import UIKit

这是我的数组列表

    var postIdArray :[String] = []
var adminIdArray :[String] = []
var titleArray :[String] = []
var descriptionArray :[String] = []
var ImageArray :[String] = []
var postDate :[String] = []
var myIndex = 0

class News__Latest_News_: BaseViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet var tabelView: UITableView!
final let urlString = "http://iccukapp.org/Api_json_format/"

override func viewDidLoad() {
super.viewDidLoad()
self.downloadJsonWithURL()
}

这里我下载JSON

        func downloadJsonWithURL() {

let url = NSURL(string: urlString)
URLSession.shared.dataTask(with: (url as URL?)!, completionHandler: {(data, response, error) -> Void in

if let jsonObj = try? JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? NSDictionary {
print(jsonObj!.value(forKey: "result") as Any)

if let ListArray = jsonObj!.value(forKey: "result") as? NSArray {
for eList in ListArray{
if let eventIDD = eList as? NSDictionary {
if let name = eventIDD.value(forKey: "post_id") {
postIdArray.append(name as! String)
}
if let name = eventIDD.value(forKey: "admin_id") {
adminIdArray.append(name as! String)
}
if let name = eventIDD.value(forKey: "title") {
titleArray.append(name as! String)
}
if let name = eventIDD.value(forKey: "description") {
descriptionArray.append(name as! String)
}
if let name = eventIDD.value(forKey: "post_iamge") {
ImageArray.append(name as! String)
}
if let name = eventIDD.value(forKey: "post_created") {
postDate.append(name as! String)
}


}
}
}

OperationQueue.main.addOperation({
self.tabelView.reloadData()


})
}
}).resume()
}

这里我配置TableView

        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return postIdArray.count
}

**这是 TableView 单元格的配置**

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

let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! NewsTableViewCell

cell.nameLabel.text = titleArray[indexPath.row]
let alinkurl = "http://iccukapp.org/assets/admin/images/"

let imagUrl = NSURL(string: "\(alinkurl)" + ImageArray[indexPath.row])


let qos = DispatchQoS(qosClass: .background, relativePriority: 0)
let backgroundQueue = DispatchQueue.global(qos: qos.qosClass)
backgroundQueue.async {
if imagUrl != nil {
let data = NSData(contentsOf: (imagUrl as URL?)!)
cell.imageLabel.image = UIImage(data: data! as Data)

}

}

return cell

}

这个函数用于执行 Segue

        func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
myIndex = indexPath.row
performSegue(withIdentifier: "newsId", sender: self)


}

}

运行应用程序后,第一次时间 TableView 显示正确的内容。但是当我转到其他页面并返回此页面时,它复制了所有内容。 高级感谢

最佳答案

我怀疑 func downloadJsonWithURL() 被调用了两次,因为这会将数据附加到您用来填充表的数组,所以您会看到重复

尝试设置:

self.postIdArray = []
self.adminIdArray = []
self.titleArray = []
self.descriptionArray = []
self.ImageArray = []
self.postDate = []

在 func downloadJsonWithURL() 开始时设置空数组,然后附加 JSON 调用的结果

关于ios - 快速返回另一页后, TableView 单元格正在复制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44687350/

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