LoginSignup
17
20

More than 5 years have passed since last update.

API通信した結果をUITableViewに反映させたい

Posted at

Almofire使ってAPI通信して、結果を配列に入れてそれをテーブルに反映させたかったのですが、普通にやると通信するより先にテーブルが作られてしまって結果として配列は空のままで何も入ってないテーブルになってしまいます。

で結果として、

UITableViewControllerを継承したViewController.swift
    var myTableView: UITableView!
    var ar: [String] = []

    override func viewDidLoad() {
        (通信する処理) {
            ar = (通信結果)
            myTableView.reloadData()
        }
    }


    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = UITableViewCell(style: .Default, reuseIdentifier: "cell")

        cell.textLabel?.text = "\(self.ar[indexPath.row])"
        // Configure the cell...
        return cell
    }

というようにUITableViewのreloadDataメソッドでテーブルが更新され、更新されたセル1行毎にtableViewメソッドが走ってセルのテキストに通信結果の文字列が表示されるようになりました。

17
20
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
17
20