1
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
}
**下面是表視圖小區配置**
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)
}
}
後運行應用程序第一次表視圖顯示正確的內容。但是,當我轉到其他頁面並返回到此頁面時,它會複製所有內容。 高級感謝
你是否在'viewDidLoad'之外的任何地方調用'downloadJsonWithURL'? – paulvs