繁体   English   中英

coreData仅打印最后一个实体(swift4)

[英]coreData only printing last entity (swift4)

我正在尝试调用coreData .color的所有条目。 问题是只有1个coreData条目被调用到标签。 我希望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
    }
}

实际上,您当前的循环将数组的最后一项设置为标签文本,因此请尝试将此值附加在一起

  for c in users {

        label.text = "\((label.text)!)+\((c.color)!)" 
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM