###前提
swift2.3でtableviewの作成を行っています。
tableviewにはncmbから取得したデータを表示するようにしてあります。
###発生している問題・質問内容
tableviewに取得した値が表示されません。
画面を回転させたりして更新してあげると表示されるので、取得自体はできています。
なので、viewdidloadの後にtableviewに値が格納されており、データが反映できていないと考えています。
そこで、どうすればtableviewを更新できるか教えて欲しいです。
###該当のソースコード
import UIKit import NCMB class ViewController: UIViewController,UITableViewDelegate, UITableViewDataSource { var tablenum : Int = 0 var member : Array<String> = [] var objectId : Array<String> = [] override func loadView() { super.loadView() // TestClassクラスを検索するNCMBQueryを作成 let query = NCMBQuery(className: "H28mm_member") // データストアの検索を実施 query.findObjectsInBackgroundWithBlock({(objects, error) in if (error != nil){ // 検索失敗時の処理 print("失敗") }else{ // 検索成功時の処理 for num in 0 ... objects.count-1{ self.member.append(objects[num].objectForKey("name") as! String) } self.tablenum = objects.count-1 } }) } override func viewDidLoad() { super.viewDidLoad() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } // セルの個数を指定するデリゲートメソッド(必須) func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return tablenum } // セルに値を設定するデータソースメソッド(必須) func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { // セルを取得する let cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier("user1", forIndexPath: indexPath) // セルに表示する値を設定する cell.textLabel!.text = member[indexPath.row] return cell } // セルが選択された時に呼ばれるデリゲートメソッド func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath){ // H28mm_memberクラスを検索するNCMBQueryを作成 let innerQuery = NCMBQuery(className:"H28mm_member") // nameフィールドの値で選択したセルと一致するデータを検索する条件を設定 innerQuery.whereKey("name", equalTo:member[indexPath.row]) // 設定した条件で検索 innerQuery.findObjectsInBackgroundWithBlock({(objects, error) in if (error != nil){ // 検索失敗時の処理 print("error") }else{ // 検索成功時の処理 // 「ud」というインスタンスをつくる。 let ud = NSUserDefaults.standardUserDefaults() // キーidに「innerQuery」の値を格納。(idは任意の文字列でok) ud.setObject(self.member[indexPath.row], forKey: "id") //udの同期 ud.synchronize() //キーがidの値をとります。 let udId : AnyObject! = ud.stringForKey("id") print(udId) // キーidの値を削除 //ud.removeObjectForKey("id") } }) } }
###開発環境
xcode8
swift2.3
ios10
cocoapod
ncmb
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/11/24 08:37
2016/11/24 08:48
2016/11/24 09:04
2016/11/24 09:08
2016/11/24 09:46 編集