ios - 三个实体分层,但在第三个中第一个不被考虑
问题描述
我有一个包含三个的应用程序,TableViews
每个应用程序都在一个单独的ViewController
. 我想将数据从传递firstVC
到。俱乐部 -> 会员 -> 交易。如果只有一个俱乐部,没有问题。第二社里有第二社的成员,一定是这样的。secondVC
thirdVC
问题
但是如果用户点击会员,在第一个俱乐部的 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")
}
应用程序的描述-
解决方案
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
}
推荐阅读
- java - 如何将链接列表分成两个其他列表,一个具有奇数索引,另一个具有偶数索引但以递归方式
- javascript - 我疯了,试图弄清楚如何将 JSON 响应作为字符串传递给 dynamodb 调用
- python - “静音”命令根本不起作用(Discord.py,重写)
- bash - 如何在 SFTP 中获取远程文件的时间戳并将其与文件本地版本的时间戳进行比较?
- python - 使用 pip 安装 Airflow
- c - 给定链表对通过更改链接进行交换
- c - nanosleep() 等待至少 15 毫秒?
- angular - 如何在一个数组中结合两个基于 ID 的 observables?
- datatable - 从 pdf 导出中的数据表标题中删除选择选项
- python - 如何将列表混合列表中的数字字符串转换为int(Python)