Ios 加载tableview数据
我在一个表视图中引入和修复数据,当x等于1时会引入到数据中,等于2时会转移到修复数据中,但当我在表视图中实现为空时,我的委托和数据源都有一个链接 以下是我的下载数据功能:Ios 加载tableview数据,ios,swift,uitableview,grand-central-dispatch,Ios,Swift,Uitableview,Grand Central Dispatch,我在一个表视图中引入和修复数据,当x等于1时会引入到数据中,等于2时会转移到修复数据中,但当我在表视图中实现为空时,我的委托和数据源都有一个链接 以下是我的下载数据功能: func laodingTableviewData() { self.suggestionTableView.isHidden = true let frame = CGRect(x: 150, y: 283, width: 80, height: 80) let color = UIColor(red
func laodingTableviewData() {
self.suggestionTableView.isHidden = true
let frame = CGRect(x: 150, y: 283, width: 80, height: 80)
let color = UIColor(red: 50.0/255.0, green: 124.0/255.0, blue: 203.0/255.0, alpha: 1)
let activityIndicatorView = NVActivityIndicatorView(frame: frame, type: .ballSpinFadeLoader, color: color, padding: 20)
self.view.addSubview(activityIndicatorView)
activityIndicatorView.startAnimating()
if self.x == 1 {
DispatchQueue.main.async {
print("Start download suggestion data...")
self.suggestions = Suggestion.downloadAllSuggestion()
self.suggestionTableView.isHidden = false
self.suggestionTableView.reloadData()
}
} else if self.y == 2 {
DispatchQueue.main.async {
print("Start download repair data...")
self.repairs = Repair.downloadAllRepair()
self.suggestionTableView.isHidden = false
self.suggestionTableView.reloadData()
}
}
activityIndicatorView.stopAnimating()
}
我已经在self.suggestions=advice.downloadAllSuggestion()
中设置了断点,我确信捕获到了数据,但无法在表视图中显示,请帮助回答,谢谢
下面是表视图函数
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "mycell", for: indexPath) as! FirstTableViewCell
if x == 1 {
cell.suggestions = suggestions[indexPath.row]
} else if y == 2 {
cell.repairs = repairs[indexPath.row]
}
return cell
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
var value = 0
switch value {
case self.x:
value = suggestions.count
case self.y:
value = repairs.count
default:
break
}
return value
}
在
numberOfRowsInSection
中,值用0初始化
然后传给开关箱。它将始终处于默认状态。因此,numberofrowsinssection
总是返回0
你的函数应该是这样的
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
var value = 0
if self.x == 1 {
value = suggestions.count
} else if self.y == 2 {
value = repairs.count
}
return value
}
据我所知,您正在根据x和y值有条件地绑定数据。您的
numberofrowsinssection
方法也将反映相同的情况
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if x == 1 {
return suggestions.count
} else if y == 2 {
return repairs.count
}
return 0
}
希望能有帮助。快乐编码 添加cellForRowAtIndexPath和NumberRowInstruction方法的代码肯定是流式的,
downloadAllSuggestion()
可能是异步的,这意味着reloadData()
在下载完成之前被调用,因为您的表视图没有update@Tj3n这听起来也不对,因为异步方法不应该有返回类型。我也被Suggestion.downloadAllSuggestion()
@Tj3n中的内容弄糊涂了。我先移动Suggestion.downloadAllSuggestion(),然后它成功地执行了正确的操作@廖豪豪 numberofrowsinssection
方法有问题。我已经更新了我的答案。谢谢你的帮助!谢谢你真的回答得很快^^