gpt4 book ai didi

ios/swift - 我无法附加表格 View 单元格

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

我想从服务器获取数据并将其显示为TableView。获取数据成功。然而,数据只能在“getDur1Data()”函数中通过追加到TableList中才能正常工作,这在类中不起作用...

所以我不知道为什么 TableView.count == 0 在那里(我之前在“TableList”中附加了“listSeparated”...)

我也希望知道为什么在 getDur1Data() 中的“print(self.Table.cout)”之前输出“print("row value (row)")”

class CustomPortController : UITableViewController {
var TableList = Array<CustomPortVO>()
var listSeparated = CustomPortVO()

func getDur1Data(){

self.TableList.append(self.listSeparated)
print(self.TableList.count) //in here, it works well so output is "1", "2", "3"..
}

override func viewDidLoad() {
super.viewDidLoad()
self.getDur1Data()
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let row = self.TableList.count
print("row value \(row)") // in here, "row value 0" is output three times
return row
}
}

已编辑 - 重写所有代码

class CustomPortController : UITableViewController {
var TableList = Array<CustomPortVO>()
var listSeparated = CustomPortVO()

override func viewWillAppear(_ animated: Bool) {


self.tableView.reloadData()

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

self.getDur1Data()

}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let row = self.TableList.count
print("row value \(row)")
return row

}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let row = self.TableList[indexPath.row]

// let cell = tableView.dequeueReusableCell(withIdentifier: "ListCell1") as! UITableViewCell
// NSLog("result=\(row.title), row index=\(indexPath.row)");

let cell = tableView.dequeueReusableCell(withIdentifier: "ListCell1") as! CustomPortTableViewCell
cell.indexNum?.text = "\(row.indexNum!)"
cell.nameOfStyle?.text = row.title
cell.percentage?.text = row.rating
return cell
}
override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
let row = self.TableList[indexPath.row]
// NSLog("result=\(row.title), row index=\(indexPath.row)");
/*
let cell = tableView.dequeueReusableCell(withIdentifier: "ListCell1") as! CustomPortTableViewCell
cell.indexNum?.text = "\(row.indexNum!)"
cell.nameOfStyle?.text = row.title
cell.percentage?.text = row.rating*/

}



func getDur1Data(){
let url1:String = "private url, sorry :)"
let url1Enc = URL(string: url1)
if let _url = url1Enc{
var request = URLRequest(url: _url)
request.httpMethod = "get"

let session = URLSession.shared
let task = session.dataTask(with: request) { (data: Data?, response: URLResponse?, error: Error?) in
guard error == nil && data != nil else{
if let err = error{
print(err.localizedDescription)
}
return
}

if let _data = data{
if let strData = NSString(data: _data, encoding: String.Encoding.utf8.rawValue){
let str = String(strData)

var separatedData = str.split(separator: "=")
for i in 1...3{
separatedData[i*4].removeFirst()
var index = separatedData[i*4].index(of:"\"")!
separatedData[i*4] = separatedData[i*4][..<index]
separatedData[i*4+1].removeFirst()
index = separatedData[i*4+1].index(of:"\"")!
separatedData[i*4+1] = separatedData[i*4+1][..<index]
print("\(separatedData[i*4])\n");
print("\(separatedData[i*4+1])\n")
}

for row in 1...3{
self.listSeparated.indexNum = row
self.listSeparated.rating = String(separatedData[row*4 + 1])
self.listSeparated.title = String(separatedData[row*4])
print(self.listSeparated.indexNum!)
print(self.listSeparated.rating!)
print(self.listSeparated.title!)
self.TableList.append(self.listSeparated)

print(self.TableList.count)
print(self.TableList[row-1])
}

DispatchQueue.main.async{
}

}

}else{
print("data nil")
}
}
task.resume()

}


}
}

当我构建这个项目时,第一次我看不到任何表格单元格。但是回到 Tableview 后,我可以看到 3 个表格单元格具有相同的数据,如下所示

1 abcd 1234
1 abcd 1234
1 abcd 1234

最佳答案

根据请求更新数组后,您需要调用 self.tableView.reloadData()

此外,在获取表中的数据时,您必须使用 viewWillAppear 而不是 viewDidLoad

关于ios/swift - 我无法附加表格 View 单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52574460/

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