私はUITableViewの問題に直面しています。リモートデータベースからUITableViewセルを入力する
私は、リモートデータベースからフェッチされたデータでそのセルを動的に埋めたいので、データが到着するまでに何度かかかります。ここ
コードは次のとおり
class MailBoxViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var tableView: UITableView!
var users: [NSDictionary] = []
override func viewDidLoad() {
super.viewDidLoad()
// call to rest API code to get all users in [NSDictionary]
(...)
// set table view delegate and data source
self.tableView.delegate = self
self.tableView.dataSource = self
}
// set number of sections within table view
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
// set number of rows for each section
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if section == 0 {
return self.users.count
}
return 0
}
// set header title for each section
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
if section == 0 {
return "Users"
}
}
// set cell content for each row
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
// deque reusable cell
let cell = tableView.dequeueReusableCellWithIdentifier("myCell", forIndexPath: indexPath) as UITableViewCell
// set item title
if indexPath.section == 0 {
cell.textLabel?.text = self.users[indexPath.row]["firstname"] as? String
}
return cell
}
}
問題はのtableView機能は各セクションの行数を設定し、各行のセルの内容を設定するために呼び出されたとき、私の[NSDictionaryの]ユーザーがまだ空であるということです。
[NSDictionary]のすべてのユーザーを取得するためのAPIコードの呼び出しが完了した後でのみ、行とセルを設定する方法はありますか?
ありがとうございました。
よろしく
はい、データソースメソッドが最初に呼び出されたときに、self.usersが空であるため、配列外の例外が原因でクラッシュしました。 – Thomi
ここでどのように外れてしまうのか分かりません。ユーザー数の配列の数を返します。ユーザーがいない場合、 'cellForRowAtIndexPath:'メソッドは呼び出されません。 – AdamPro13
あなたは正しいです。私は今それを得た、たくさんありがとう! – Thomi