首页 > 解决方案 > 三个实体分层,但在第三个中第一个不被考虑

问题描述

我有一个包含三个的应用程序,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")
            }

应用程序的描述-

标签: iosswiftxcodeuitableviewcore-data

解决方案


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
}

推荐阅读