2017-11-02 10 views
1

私は静的なものに1つのtableViewからデータを渡そうとしています。私はセグを使用せず、代わりにinstantiateviewcontrollerwithidentifierを使用しています、私はどこに間違っているのか分かりません。私はクラウドキットを使用しており、icloudからckRecordsを取得しています。私はコレクションビューを最初にどこにセルをタップし、そのセルに対応するブックを表示します。しかし、それらがセルに表示されているときは、セルをタップしてdetailViewControllerを表示します。それは現時点では機能していないので、その理由を頭で知ることはできません。ストーリーボードを使用してtableViews間でデータを渡すinstantiateviewcontrollerwithidentifier

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

これは現在の結果ですか? 'DetailViewController'が表示されない、またはデータがなくても表示されますか? –

+0

@ KamilSzostakowskiそれはちょうど 'の場合、ディテールVC =ストーリーボード?スタンドアロンViewController(withIdentifier:" DetailVC ")としてクラッシュ? DetailViewController'。クラッシュして何も表示されません。 – user6574269

+0

'DetailViewController'が' BooksViewController'と同じストーリーボードにあることは確かですか? 'DetailViewController'の正しいソーリーボードIDを設定しましたか?クラッシュメッセージを投稿してください。 –

答えて

1

UIViewControllerstoryboardプロパティは、ビューコントローラが発信されたストーリーボードを表します。同じストーリーボードから別のView Controllerをロードする場合は、それを使用できます。

しかし、別のストーリーボードからUIViewControllerをロードする場合は、最初にUIStoryboardインスタンスを作成する必要があります。

このようにすることができます。

UIStoryboard(name: "YourStoryboardName", bundle: nil).instantiateViewController(withIdentifier: "DetailVC") 
+0

は魅力的です。歓声メイト – user6574269

関連する問題