在快速

时间:2017-06-21 22:40:43

标签: ios swift uitableview

    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)


        }

    }

运行应用程序后第一次表视图显示正确的内容。但当我转到其他页面并返回此页面时,重复所有内容。     高级谢谢

1 个答案:

答案 0 :(得分:0)

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

尝试设置:

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

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