ViewControllerに表示してあるデータを初期化する方法はありますか?
はじめにViewControllerに表示したデータを初期化して、新しいデータを表示したいと思っています。初期化ではなく更新でもいいのですが、どちらにしても方法が分からなくて。
ご存知の方は、ご教授ください。
[追記]
ちなみに別のControllerからデータを更新するのではなく、ViewController内でリアルタイムに更新したデータを表示したいと考えています。
よろしくお願いします。
気になる質問をクリップする
クリップした質問は、後からいつでもMYページで確認できます。
またクリップした質問に回答があった際、通知やメールを受け取ることができます。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
回答1件
0
ベストアンサー
ViewControllerといっても様々な状態があるのでどう答えればいいのか…
更新したいものがUILabelやUItextViewなら、新しいデータを取得し終わったら、そのデータを
lang
1myLabel.text = "aaaa"
のように.textプロパティに入れてやれば変わります。
UITableViewの場合は
lang
1self.tableView.reloadData()
などで全cellを更新させたり、他にも指定のセルだけ更新や追加させたりできるメソッドが用意されているのでそれを使えばできます。
ちなみにUIの変更はメインスレッドで行わなければならないので注意です。
----------追記----------
解答が長く、汚くなりそうなのでこちらでコメントします。
こちらで再現しようとしてみました。
![結果]WIDTH:600
storyboardの部分は上のcollectionViewのtagが1、下のcollectionViewが2です。
残りは察してください...
lang
1import UIKit 2 3class viewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource { 4 5 6 @IBOutlet weak var myCollectionView: UICollectionView! 7 @IBOutlet weak var secondCollection: UICollectionView! 8 var object:Array<String> = [] 9 var object2:Array<String> = [] 10 11 override func viewDidLoad() { 12 super.viewDidLoad() 13 14 // Cellに使われるクラスを登録. 15 myCollectionView.registerClass(CustomUICollectionViewCell.self, forCellWithReuseIdentifier: "MyCell") 16 17 myCollectionView.delegate = self 18 myCollectionView.dataSource = self 19 20 secondCollection.registerClass(CustomUICollectionViewCell.self, forCellWithReuseIdentifier: "MyCell") 21 secondCollection.delegate = self 22 secondCollection.dataSource = self 23 24 object = ["a","b","c"] 25 object2 = ["1", "2", "3", "4"] 26 27 } 28 29 /* 30 Cellが選択された際に呼び出される 31 */ 32 func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { 33 if collectionView.tag == 1 { 34 println("\(indexPath.row):\(object[indexPath.row])") 35 } 36 else if collectionView.tag == 2 { 37 println("\(indexPath.row):\(object2[indexPath.row])") 38 } 39 } 40 41 /* 42 Cellの総数を返す 43 */ 44 func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 45 if collectionView.tag == 1 { 46 return object.count 47 } 48 else if collectionView.tag == 2 { 49 return object2.count 50 } 51 else { 52 println("error") 53 return 0 54 } 55 } 56 57 /* 58 Cellに値を設定する 59 */ 60 func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 61 62 let cell : CustomUICollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("MyCell", forIndexPath: indexPath) as! CustomUICollectionViewCell 63 if collectionView.tag == 1 { 64 cell.textLabel?.text = object[indexPath.row] 65 } 66 else if collectionView.tag == 2 { 67 cell.textLabel?.text = object2[indexPath.row] 68 } 69 70 return cell 71 } 72 73 @IBAction func changeData(sender: AnyObject) { 74 println("call change") 75 object[0] = "d" 76 object[1] = "e" 77 object[2] = "f" 78 object.append("g") 79 80 object2[0] = "10" 81 object2[1] = "20" 82 object2[2] = "30" 83 object2[3] = "40" 84 object2.append("50") 85 86 println(object) 87 println(object2) 88 89 } 90 91 @IBAction func reloadData(sender: AnyObject) { 92 println("call refresh") 93 self.myCollectionView.reloadData() 94 self.secondCollection.reloadData() 95 } 96 97} 98
lang
1import UIKit 2 3class CustomUICollectionViewCell : UICollectionViewCell{ 4 5 var textLabel : UILabel? 6 7 required init(coder aDecoder: NSCoder) { 8 super.init(coder: aDecoder) 9 } 10 11 override init(frame: CGRect) { 12 super.init(frame: frame) 13 14 // UILabelを生成. 15 textLabel = UILabel(frame: CGRectMake(0, 0, frame.width, frame.height)) 16 textLabel?.text = "nil" 17 textLabel?.backgroundColor = UIColor.whiteColor() 18 textLabel?.textAlignment = NSTextAlignment.Center 19 20 // Cellに追加. 21 self.contentView.addSubview(textLabel!) 22 } 23 24}
手抜き感満載ですが、こんなかんじで2つのcollectionViewを更新出来ました。
collectionViewのtagで動きを分けました。
質問者様がどのように使っているのかわかりませんが、参考になれば。
投稿2015/05/01 06:44
編集2015/05/04 14:58総合スコア157
あなたの回答
tips
太字
斜体
打ち消し線
見出し
引用テキストの挿入
コードの挿入
リンクの挿入
リストの挿入
番号リストの挿入
表の挿入
水平線の挿入
プレビュー
質問の解決につながる回答をしましょう。 サンプルコードなど、より具体的な説明があると質問者の理解の助けになります。 また、読む側のことを考えた、分かりやすい文章を心がけましょう。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2015/05/01 07:26
2015/05/01 08:45
2015/05/01 09:52
2015/05/01 11:31
2015/05/02 01:23
2015/05/02 07:20
2015/05/04 15:00
2015/05/07 01:03