【问题标题】:Three entities hierarchical but in the third the first isn't accounted三个实体分层,但在第三个中第一个不被考虑
【发布时间】:2018-07-07 16:15:24
【问题描述】:

我有一个包含三个TableViews 的应用程序,每个应用程序都位于一个单独的ViewController 中。我想将数据从firstVC 传递到secondVCthirdVC。俱乐部 -> 会员 -> 交易。如果只有一个俱乐部,没有问题。 第二社里有第二社的成员,必须是这样的。

问题

但如果用户点击会员,则在第一家具乐部的indexPath上有来自第一家具乐部的会员的交易。

我的想法

因此,我必须将俱乐部也纳入交易。但我的解决方案只是抛出了错误。

所以也许你有一个很好的解决方案。我在这里找到了解决方案,但这对我没有帮助。
这是我的TableView 代码:

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return member?.transactions?.count ?? 0
        }

        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell = tableViewTransaction.dequeueReusableCell(withIdentifier: "transactionCell", for: indexPath)
            if let transaction = member?.transactions?[indexPath.row] {

                cell.textLabel?.text = transaction.reason
                let moneystring = String(transaction.money)
                cell.detailTextLabel?.text = moneystring + " Fr."

                if transaction.money < 0.0 {
                    cell.detailTextLabel?.textColor = UIColor.red
                }
                if transaction.money > 0.0 {
                    cell.detailTextLabel?.textColor = UIColor(red: 0, green: 110/255, blue: 0, alpha: 0.8)
                }


            }
            return cell
    }

俱乐部获取:

guard let appDelegate = UIApplication.shared.delegate as? AppDelegate
            else {
            return
        }
        let managedContext = appDelegate.persistentContainer.viewContext
        let fetchrequest: NSFetchRequest <Verein> = Verein.fetchRequest()

        do {
            clubs = try managedContext.fetch(fetchrequest)
            tableViewClub.reloadData()
        } catch {
            print("Could not fetch")

会员获取:

 guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else {
                return
            }
            let managedContext = appDelegate.persistentContainer.viewContext
            let fetchrequest: NSFetchRequest<Member> = Member.fetchRequest()

            do {
                members = try managedContext.fetch(fetchrequest)

                self.tableViewMember.reloadData()
            } catch {
                print("Could not fetch")
            }

应用说明-

【问题讨论】:

  • 你遇到了什么错误
  • @V_rohit club.members 没有会员交易
  • 你能展示你的数据模式吗?l(即你定义的对象)以及你用来填充它的数据示例。以及您看到的错误。
  • @AshleyMills 我将核心数据模型添加到问题中
  • @AshleyMills 我的强项是显示交易,他们属于哪个俱乐部,因为我只是寻找会员。我想将 member.transactions 设为 club.members.transactions,这给了我一个错误,即这不是它的成员

标签: ios swift xcode uitableview core-data


【解决方案1】:

UITableView 单元格被重复使用,因此如果您加载以前使用过的单元格(其标签已设置),则可以将其用于新的单元格。因为您仅在找到事务时才设置单元格的标签,所以如果未找到事务,则单元格将仅显示先前加载的数据。你需要做这样的事情。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableViewTransaction.dequeueReusableCell(withIdentifier: "transactionCell", for: indexPath)
    // clear the cell
    cell.textLabel?.text = "" // blank or whatever the default should be
    cell.detailTextLabel?.text = ""

    // load any new data
    if let transaction = member?.transactions?[indexPath.row] {

        cell.textLabel?.text = transaction.reason
        let moneystring = String(transaction.money)
        cell.detailTextLabel?.text = moneystring + " Fr."

        if transaction.money < 0.0 {
            cell.detailTextLabel?.textColor = UIColor.red
        }
        if transaction.money > 0.0 {
            cell.detailTextLabel?.textColor = UIColor(red: 0, green: 110/255, blue: 0, alpha: 0.8)
        }


    }
    return cell
}

【讨论】:

  • 这是有道理的,但它并没有改变任何东西。 indexPath 0.0 处的会员数据和第一俱乐部的数据与indexPath 0.0 和第二俱乐部的会员数据相同。
  • 那么您获取的数据有误。请包含您用于获取俱乐部和会员的代码。
  • 我将其添加到问题中
猜你喜欢
  • 2017-03-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-20
  • 1970-01-01
  • 2016-09-11
  • 2015-06-02
  • 1970-01-01
相关资源
最近更新 更多