gpt4 book ai didi

Swift Tableview 刷新错误

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

我收到此错误:

image 1

这是我的代码: image 2

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

But in which phase it falls to the fault I could not solve that part

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()
}

}

I am taking data from web service in this section

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()
}

最佳答案

我猜问题出在您尝试填充表格 View 的代码部分。因此,可能的解决方案可以是在 tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) 委托(delegate)方法中检查 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

关于Swift Tableview 刷新错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42632969/

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