質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Q&A

解決済

2回答

405閲覧

【Swift4.0】データベースから取得したTableViewCellが反映されない

MasatoTunashima

総合スコア8

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

0グッド

0クリップ

投稿2018/02/15 05:24

前提・実現したいこと

データベース(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にデータが反映されるところでつまづいている」というのが僕の認識です。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

見当違いでしたらご容赦ください。

↓の部分を

swift

1 //セルの数をall_itemsのクラスに入っているデータの数に合わせる 2 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 3 return all_items.count 4 }

swift

1 //セルの数をall_itemsのクラスに入っているデータの数に合わせる 2 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 3 return all_items.count 4 print("セルの数:",all_items.count) 5 }

にして。プリントされたセルの個数が、TableViewに表示されていセルの数と一致しているとしたら、カスタムセルの設定が原因ではないでしょうか?
カスタムセルはどのよう設定してありますか?

swift

1 func tableView(_ tableView:UITableView, cellForRowAt indexPath:IndexPath) -> UITableViewCell { 2 let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TopPageTableViewCell 3 4 print(cell) 5 6 cell.setCell(itemInf: all_items[indexPath.row]) 7 8 return cell 9 }

の中の"cell"と"TopPageTableViewCell"と"setCell"に対応する部分が存在するかが原因でしょうが、
エラーがでてないとしたら、メソッド"setCell"がどのように記述してあるかでしょうか?

投稿2018/02/15 06:01

退会済みユーザー

退会済みユーザー

総合スコア0

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

ベストアンサー

データの読み込みが完了した時点でテーブルをリロードして下さい。
reloadData()

投稿2018/02/15 05:47

fuzzball

総合スコア16731

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

MasatoTunashima

2018/02/15 06:28

ご回答ありがとうございます。非常にありがたいです。 self.TableView.reloadData() を記述したのですが、該当のページを開く際にエラーが発生してしまいました。 ーー libc++abi.dylib: terminating with uncaught exception of type NSException (lldb) ーー こちらどのような原因が考えられるでしょうか?回答いただけますと幸いです。
fuzzball

2018/02/15 06:36

「該当のページを開く際」とはどういう意味でしょうか? reloadData()はどこに記述したのでしょうか? 記述しなければ落ちない(エラーは発生しない)のでしょうか?
MasakiHori

2018/02/15 08:49

GUI更新系はメインスレッドで DispatchQueue.main.sync { self.TableView.reloadData() } ですかね この場合は asyncにする意味はなさそうです
MasatoTunashima

2018/02/22 09:11

おかげさまで解決することができました。ありがとうございます!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問