Swift Tableview刷新错误

时间:2017-03-06 18:29:16

标签: swift uitableview

我收到此错误:

image 1

这是我的代码: image 2

我在项目的tableView部分使用refresh。刷新期间可能导致此错误的原因是什么?

  

但是在哪个阶段,我无法解决这个问题

var kategoriId = ""
    var refresher = UIRefreshControl()

    var arrayKonularData = [konularData]()
    let singleton = konularClass.sharedGlobal

    override func viewDidLoad() {
        super.viewDidLoad()

        refresher.attributedTitle = NSAttributedString(string: "Yükleniyor")
        refresher.addTarget(self, action: #selector(KonuDetayViewController.refresh), for: UIControlEvents.valueChanged)
        self.tableview.addSubview(refresher)


        KonulariGetir(sirala: "order by tarih desc")
        navigationController?.delegate = self
        tableview.layer.cornerRadius = 10
    }
    func refresh()
    {
        DispatchQueue.main.async {
            if self.segmentControl.selectedSegmentIndex == 0
            {
                self.arrayKonularData.removeAll()
                self.KonulariGetir(sirala: "order by tarih desc")
            }
            if self.segmentControl.selectedSegmentIndex == 1
            {
                self.arrayKonularData.removeAll()
                self.KonulariGetir(sirala: "order by indirimpuani desc")
            }

        }
                DispatchQueue.main.async {
            self.refresher.endRefreshing()
        }

    }
  

我在本节中从网络服务中获取数据

func KonulariGetir(sirala:String)
    {
        var request = URLRequest(url: URL(string:"http://212.xxx.xxx.xxx:7001/IndirimiKovala/KonuGetir")!)
        request.httpMethod = "POST"
        let postString = "filtre="+sirala
        request.httpBody = postString.data(using: .utf8)
        let task = URLSession.shared.dataTask(with: request) { (data, response, error) in

            if error != nil
            {
                print("error")
            }

            if let urlContent = data
            {

                do
                {
                    let jsonResult = try JSONSerialization.jsonObject(with: urlContent, options: JSONSerialization.ReadingOptions.mutableContainers) as AnyObject

                    if let gelenDizi = jsonResult as? NSArray
                    {
                        for i in 0..<gelenDizi.count
                        {

                            if let baslik = (gelenDizi[i] as? NSDictionary)?["baslik"] as? String
                            {
                                self.singleton.baslik = baslik
                            }

                            if let indirimPuani = (gelenDizi[i] as? NSDictionary)?["indirimpuani"] as? Int
                            {
                                self.singleton.indirimPuani = String(indirimPuani)
                            }
                            if let konuId = (gelenDizi[i] as? NSDictionary)?["id"] as? Int
                            {
                                self.singleton.konuId = String(konuId)
                            }
                            if let haberVeren = (gelenDizi[i] as? NSDictionary)?["uye"] as? String
                            {
                                self.singleton.haberVerenUye = haberVeren
                            }
                            if let gelenTarih = (gelenDizi[i] as? NSDictionary)?["tarih"] as? String
                            {
                                self.singleton.tarih = gelenTarih
                            }
                            if let gelenAktif = (gelenDizi[i] as? NSDictionary)?["aktif"] as? Int
                            {
                                self.singleton.aktif = gelenAktif
                            }


                            self.arrayKonularData.append(konularData.init(baslik: self.singleton.baslik, indirimPuani: self.singleton.indirimPuani, konuId: self.singleton.konuId,haberVeren:self.singleton.haberVerenUye , tarih:self.singleton.tarih,aktif:self.singleton.aktif))


                        }

                    }
                    DispatchQueue.main.async {
                        self.tableview.reloadData()
                    }


                }
                catch
                {
                    print("server hatası")
                }
            }
        }
        task.resume()
    }

1 个答案:

答案 0 :(得分:0)

我想这个问题来自您尝试填充tableview的代码部分。所以可能的解决方案可以在tableView(_ tableView:UITableView,cellForRowAt indexPath:IndexPath)委托方法中检查arrayKonularData数组是不是这样的空

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {
        let cell = tableView.dequeueReusableCell(withIdentifier: orderCell, for: indexPath)


        if !arrayKonularData.isEmpty {
            .....// Your code
        }

        return cell
    }

另一种解决方案(在我的情况下,我将是正确的解决方案)添加

的完成功能
func KonulariGetir(sirala:String)

并在完成方法中重新加载tableview