[英]coreData only printing last entity (swift4)
I am trying to call all of my entries of coreData .color. 我正在尝试调用coreData .color的所有条目。 The problem is only 1 entry of coreData is being called to the label. 问题是只有1个coreData条目被调用到标签。 I would like all of the entities of coreData to be printed on the label not just the latest one, which the code is currently doing. 我希望coreData的所有实体都可以打印在标签上,而不仅仅是当前代码正在执行的最新操作。
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var tableView: UITableView!
@IBOutlet var label: UILabel!
var users = [User]()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
tableView.dataSource = self
if CDHandler.fetchObject() != nil {
users = CDHandler.fetchObject()!
tableView.reloadData()
}
}
}
extension ViewController: UITableViewDataSource {
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return users.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: .subtitle, reuseIdentifier: nil)
cell.textLabel?.text = users[indexPath.row].username
for c in users {
label.text = c.color
}
return cell
}
}
Actually your current loop sets the last item of the array to the label text , so Try this to append values together 实际上,您当前的循环将数组的最后一项设置为标签文本,因此请尝试将此值附加在一起
for c in users {
label.text = "\((label.text)!)+\((c.color)!)"
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.