前提・実現したいこと
データベース(mBaaS)からデータを取得して、TableViewCellに反映させたいです。
TableViewCellに反映させたいall_itemsという配列までにはデータが読み込めているようなのですが、起動してみるとセルの中に文字や写真のデータがあらわれません。
とくにエラーは発生していません。
ご回答いただけると幸いです。
該当のソースコード
swift4.0
swift
1import UIKit 2import NCMB 3 4 5class TopPageViewController: UIViewController, UITableViewDelegate,UITableViewDataSource,UISearchBarDelegate{ 6 7 @IBOutlet weak var SearchBar: UISearchBar! 8 @IBOutlet weak var TableView: UITableView! 9 10 var accountInterface: [String: Any] = [:] 11 let currentUser = NCMBUser.current() 12 13 var all_items:[itemInf] = [itemInf]() 14 15 16 17 //データを取得する 18 let query = NCMBQuery(className: "TestClass") 19 20 override func viewDidLoad() { 21 super.viewDidLoad() 22 23 print("all_items: (all_items)") 24 //print("itemInf: (String(describing: self.itemInf))") 25 all_items.append(itemInf.init(itemTitle: "a", itemPrice: "b", itemSpeaker: "c", itemImage: #imageLiteral(resourceName: "icon-rec.png"))) 26 TableView.dataSource = self 27 TableView.delegate = self as? UITableViewDelegate 28 SearchBar.delegate = self 29 30 SearchBar.enablesReturnKeyAutomatically = false 31 32 } 33 34 35 36 func fetchAllItems(){ 37 38 let query = NCMBQuery(className: "itemClass") 39 query?.findObjectsInBackground({(objects, error)in 40 if error != nil{ 41 print("ユーザー情報の読み込みに失敗") 42 print(error!) 43 } else { 44 for x in 0..<(objects?.count)!{ 45 let object = objects![x]as! NCMBObject 46 print(object) 47 var title: String? 48 var price :String? 49 var desc: String? 50 var img: UIImage? 51 if object.object(forKey: "item_title") != nil{ 52 title = object.object(forKey: "item_title")as? String 53 print(title) 54 }else{ 55 self.accountInterface["title"] = "タイトル" 56 } 57 58 if object.object(forKey: "item_description") != nil{ 59 desc = object.object(forKey:"item_description")as? String 60 }else{ 61 self.accountInterface["userName"] = "スピーカー" 62 } 63 64 if object.object(forKey:"item_price") != nil{ 65 //self.itemInf?.itemPrice = object.object(forKey:"item_price")as! String 66 price = object.object(forKey:"item_price")as? String 67 }else{ 68 self.accountInterface["price"] = "値段" 69 } 70 71 if object.object(forKey:"thumbnailName") != nil{ 72 img = object.object(forKey:"thumbnailName")as! UIImage 73 }else{ 74 self.accountInterface["thumbnailName"] = "サムネイル" 75 } 76 self.all_items.append(itemInf.init(itemTitle: title!, itemPrice: price! 77 , itemSpeaker: desc!, itemImage: "" )) 78 print(self.accountInterface) 79 print(self.all_items) 80 81 }} 82 }) 83 84 return () 85 } 86 87 func numberOfSections(in tableView: UITableView) -> Int { 88 return 1 89 } 90 91 92 //セルの数をall_itemsのクラスに入っているデータの数に合わせる 93 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 94 return all_items.count 95 } 96 97 98 99 func tableView(_ tableView:UITableView, cellForRowAt indexPath:IndexPath) -> UITableViewCell { 100 let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TopPageTableViewCell 101 102 print(cell) 103 104 cell.setCell(itemInf: all_items[indexPath.row]) 105 106 return cell 107 } 108 109 110 111 112 override func didReceiveMemoryWarning() { 113 super.didReceiveMemoryWarning() 114 // Dispose of any resources that can be recreated. 115 116 } 117 118 func tableView(_ tableView:UITableView, didSelectRowAt indexpath: IndexPath){ 119 120 tableView.deselectRow(at: indexpath,animated: true) 121 122 123 self.present(ProductPageViewController(), animated:true, completion: nil) 124 }
補足情報(FW/ツールのバージョンなど)
データベースからデータを引き出すところまではうまくいっています。
また、all_itemsの配列にもデータは反映されていました。
「cellにデータが反映されるところでつまづいている」というのが僕の認識です。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。