我正在使用核心数据保存2个表信息,其中一个是“客户”,另一个是“预定票证”。一位顾客可以预订许多票。我想获取此相关数据并在表视图中填充。我正在获取所有票证数据,但我想要特定用户的票证信息。例如users.ticket.passangerName。但是我在这里变成零。
import UIKit
import CoreData
class BookHistoryVC: UIViewController {
@IBOutlet weak var tableView: UITableView!
var tickets: [BookedTickets]? {
didSet{
tableView.reloadData()
}
}
var users: [Customer] = []
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
// Do any additional setup after loading the view.
self.fetch { (complete) in
if complete {
guard let tickets = tickets else {
return
}
if (tickets.count) >= 1 {
//tableView.isHidden = false
print(tickets as Any)
} else {
return
}
} else {
//tableView.isHidden = true
}
}//Fetch Complete
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func fetch(completion: (_ complete : Bool) -> ()) {
guard let managedContext = appDelegate?.persistentContainer.viewContext else { return }
let fetchRequest1 = NSFetchRequest<NSFetchRequestResult>(entityName: "Customer")
let fetchRequest2 = NSFetchRequest<NSFetchRequestResult>(entityName: "BookedTickets")
do {
users = try managedContext.fetch(fetchRequest1) as! [Customer]
tickets = try ((managedContext.fetch(fetchRequest2) as? [BookedTickets]))
print("Succesfully Fetched")
tableView.reloadData()
completion(true)
} catch {
debugPrint("Could Not Fetch:\(error.localizedDescription)")
completion(false)
}
}
}
extension BookHistoryVC: UITableViewDelegate, UITableViewDataSource{
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection
section: Int) -> Int {
guard let tickets = tickets else {
return 0
}
return tickets.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "BookHistoryVCCell", for: indexPath) as? BookHistoryVCCell else { return UITableViewCell()}
guard let tickets = tickets else {
return UITableViewCell()
}
let ticket = tickets[indexPath.row]
cell.configureCell(ticket: ticket)
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 400
}
}
在单元格中...
import UIKit
class BookHistoryVCCell: UITableViewCell {
@IBOutlet weak var bookerNameLabel: UILabel!
@IBOutlet weak var ageLabel: UILabel!
@IBOutlet weak var passangerNameLabel: UILabel!
@IBOutlet weak var ticketNumberLabel: UILabel!
@IBOutlet weak var fromLabel: UILabel!
@IBOutlet weak var toLabel: UILabel!
@IBOutlet weak var createdLabel: UILabel!
var jsonNamesArr: [Any] = []
var jsonAgesArr: [Any] = []
var details = ""
func configureCell(ticket: BookedTickets) {
let fullNames: String = ticket.passangerName!
let dataNameArray = fullNames.data(using: .utf8)
let fullAges: String = ticket.age!
let dataAgeArray = fullAges.data(using: .utf8)
do {
if let jsonNameArray = try JSONSerialization.jsonObject(with: dataNameArray!, options: .allowFragments) as? [Any] {
jsonNamesArr = jsonNameArray
print(jsonNameArray)
}
} catch {
// #error()
print("Error")
}
do {
if let jsonAgeArray = try JSONSerialization.jsonObject(with: dataAgeArray!, options: .allowFragments) as? [Any] {
jsonAgesArr = jsonAgeArray
print(jsonAgeArray)
}
} catch {
print("Error")
}
for (name, age) in zip(jsonNamesArr, jsonAgesArr) {
details = details + ("\(name) of \(age)\n")
print("\(name): \(age)")
}
self.bookerNameLabel.text! = Utilities.getUserName()
//self.ageLabel.text! = "Ages: \(ticket.age!)"
self.passangerNameLabel.text! = "Passangers Details:\n\(details)"
self.fromLabel.text! = "From: \(ticket.fromDestination!)"
self.toLabel.text! = "To: \(ticket.toDestination!)"
self.createdLabel.text! = "Booked Date: \(ticket.created_at!)"
self.ticketNumberLabel.text! = "Ticket Number: \(ticket.uniqueTicketNumber!)"
}
}
我的桌子图像..
答案 0 :(得分:1)
您需要一个谓词进行过滤,例如,您正在寻找name
中customer
为John Doe的所有票证
let fetchRequest2 = NSFetchRequest<NSFetchRequestResult>(entityName: "BookedTickets")
let name = "John Doe"
fetchRequest2.predicate = NSPredicate(format: "customer.name = %@", name)
并且不需要在fetch
中使用完成块。核心数据方法fetch(_ request: NSFetchRequest)
同步。
答案 1 :(得分:0)
由于没有在用户对象中添加票证对象,因此我得到了零客户。为此,我创建了Singleton类并创建了Customer属性。
import Foundation
class Singleton {
static var user: Customer!
}
并将客户对象分配给登录页面中的单例属性。
//Singleton
Singleton.user = user
现在携带当前用户对象的单例对象。在保存故障单详细信息的同时,添加以下行Singleton.user.addToTickets(ticket)
You can refer this link
https://github.com/learnasucan/BookTicket.git