我试图将数据从一个tableView传递到静态一个。我没有使用segue而是使用instantiateviewcontrollerwithidentifier
,我不知道我在哪里出错了。我正在使用cloudkit并从icloud获取ckRecords。我有一个collectionView首先我点击一个单元格并显示与该单元格对应的书籍。但是当它们显示在单元格中时,我想点击单元格来调出detailViewController。它现在不工作,无法理解为什么。
import CloudKit
class DetailViewController: UITableViewController {
@IBOutlet weak var aboutBookLabel: UILabel!
@IBOutlet weak var bookLengthLabel: UILabel!
@IBOutlet weak var amazonPriceLabel: UILabel!
@IBOutlet weak var ebayPriceLabel: UILabel!
@IBOutlet weak var websitePriceLabel: UILabel!
@IBOutlet weak var authorLabel: UILabel!
@IBOutlet weak var bookTitleLabel: UILabel!
@IBOutlet weak var bookImageView: UIImageView!
var bookRecord: CKRecord?
override func viewDidLoad() {
super.viewDidLoad()
populateData()
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
cell.backgroundColor = UIColor(red: 27.0/255.0, green: 28.0/255.0 , blue: 32.0/255.0, alpha: 1.0)
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
}
func populateData() {
aboutBookLabel.text = bookRecord?.object(forKey: "description") as? String
bookLengthLabel.text = bookRecord?.object(forKey: "length") as? String
amazonPriceLabel.text = bookRecord?.object(forKey: "amazon") as? String
ebayPriceLabel.text = bookRecord?.object(forKey: "ebay") as? String
websitePriceLabel.text = bookRecord?.object(forKey: "authorPrice") as? String
authorLabel.text = bookRecord?.object(forKey: "author") as? String
bookTitleLabel.text = bookRecord?.object(forKey: "name") as? String
if let imageFile = bookRecord?.object(forKey: "image") as? CKAsset {
bookImageView.image = UIImage(contentsOfFile: imageFile.fileURL.path)
}
}
}
class BooksViewController: UIViewController {
@IBOutlet weak var tableView: UITableView!
let cellIdentifier = "bookCell"
var books = [CKRecord]()
var sectorTitle = "language"
override func viewDidLoad() {
super.viewDidLoad()
registerNib()
fetchBooksFromCloud()
configureTableView()
}
func fetchBooksFromCloud() {
books = [CKRecord]()
books.removeAll()
tableView.reloadData()
let cloudContainer = CKContainer.default()
let publicDatabase = cloudContainer.publicCloudDatabase
let predicate = buildBookPredicate()
let query = CKQuery(recordType: "Book", predicate: predicate)
query.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
let queryOperation = CKQueryOperation(query: query)
queryOperation.desiredKeys = ["name", "author", "image", "format", "description", "length", "amazon", "ebay", "authorPrice"]
queryOperation.queuePriority = .veryHigh
queryOperation.resultsLimit = 25
queryOperation.recordFetchedBlock = { (record) -> Void in
self.books.append(record)
}
DispatchQueue.global(qos: .userInteractive).async {
queryOperation.queryCompletionBlock = { (cursor, error) -> Void in
if let error = error {
print("Download failed \(error.localizedDescription)")
return
}
print("Download Successful")
OperationQueue.main.addOperation {
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
}
}
queryOperation.qualityOfService = .userInteractive
publicDatabase.add(queryOperation)
}
private func buildBookPredicate() -> NSPredicate {
return NSPredicate(format:"self contains '\(sectorTitle)'")
}
extension BooksViewController: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return books.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier , for: indexPath) as! BookCell
let book = books[indexPath.row]
let bookName = book.object(forKey: "name") as? String
let authorName = book.object(forKey: "author") as? String
let image = book.object(forKey: "image") as? CKAsset
let formatName = book.object(forKey: "format") as? String
cell.nameLabel.text = bookName
cell.authorLabel.text = authorName
cell.formatLabel.text = formatName
cell.bookImageName.image = UIImage(contentsOfFile: image!.fileURL.path)
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let record = books[indexPath.row]
if let detailVC = storyboard?.instantiateViewController(withIdentifier: "DetailVC") as? DetailViewController {
detailVC.bookRecord = record
navigationController?.pushViewController(detailVC, animated: true)
}
tableView.deselectRow(at: indexPath, animated: true)
}
答案 0 :(得分:1)
storyboard
的{{1}}属性表示视图控制器所源自的故事板。如果要从同一故事板加载另一个视图控制器,则可以使用它。
但是,如果您要从其他故事板加载UIViewController
,则必须先创建UIViewController
实例。
你可以这样做。
UIStoryboard