使用函数提取数据后如何填充UItableview?
How to fill-in an UItableview after extracting data with a function?
我在填写 table 视图的单元格时遇到问题:
我想在函数 Fetch() 完成运行后填充单元格,然后 arrayComponents 由我想在我的单元格中显示的信息填充:
var arrayComponents:Array<String> = Array(repeating: "", count: 4)
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return arrayComponents.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "MyCell", for: indexPath)
cell.textLabel!.text = arrayComponents[indexPath.row]
cell.imageView!.image = image
return cell
}
func fetch(jan: String){
self.fetchAll { result in
switch result {
case .failure(let error):
print(error)
case .success(let values):
self.arrayData = values
self.arrayComponents = ["\(self.arrayData[1])","\(self.arrayData[2])","\(self.arrayData[3])","\(self.arrayData[4])"]
}
}
}
case .failure(let error):
print("\(error) in fetch")
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
fetch(jan: dataFromFirst)
tableView.dataSource = self
tableView.delegate = self
}
}
如何在 fetch() 完成并返回值后加载 table 视图?
获取后重新加载 tableView 数据
func fetch(jan: String){
self.fetchAll { result in
switch result {
case .failure(let error):
print(error)
case .success(let values):
self.arrayData = values
self.arrayComponents = ["\(self.arrayData[1])","\(self.arrayData[2])","\(self.arrayData[3])","\(self.arrayData[4])"]
}
}
//Reload data
tableView.reloadData()
}
我在填写 table 视图的单元格时遇到问题:
我想在函数 Fetch() 完成运行后填充单元格,然后 arrayComponents 由我想在我的单元格中显示的信息填充:
var arrayComponents:Array<String> = Array(repeating: "", count: 4)
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return arrayComponents.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "MyCell", for: indexPath)
cell.textLabel!.text = arrayComponents[indexPath.row]
cell.imageView!.image = image
return cell
}
func fetch(jan: String){
self.fetchAll { result in
switch result {
case .failure(let error):
print(error)
case .success(let values):
self.arrayData = values
self.arrayComponents = ["\(self.arrayData[1])","\(self.arrayData[2])","\(self.arrayData[3])","\(self.arrayData[4])"]
}
}
}
case .failure(let error):
print("\(error) in fetch")
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
fetch(jan: dataFromFirst)
tableView.dataSource = self
tableView.delegate = self
}
}
如何在 fetch() 完成并返回值后加载 table 视图?
获取后重新加载 tableView 数据
func fetch(jan: String){
self.fetchAll { result in
switch result {
case .failure(let error):
print(error)
case .success(let values):
self.arrayData = values
self.arrayComponents = ["\(self.arrayData[1])","\(self.arrayData[2])","\(self.arrayData[3])","\(self.arrayData[4])"]
}
}
//Reload data
tableView.reloadData()
}