从远程数据库填充 UITableView 单元格
Filling UITableView cell from remote database
我遇到了 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] 用户仍然是空的。
只有在调用 rest API 代码以获取 [NSDictionary] 中的所有用户后,我该如何设置行和单元格?
感谢您的任何反馈。
此致
当您收到 API 的响应时,您应该设置 self.users = arrayOfUsersYouReceivedFromServer
然后调用 self.tableView.reloadData()
。这个
填充users
后,调用tableView.reloadData()
。这将再次调用您的数据源方法。
完成获取用户后调用 tableView.reloadData()。
我遇到了 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] 用户仍然是空的。
只有在调用 rest API 代码以获取 [NSDictionary] 中的所有用户后,我该如何设置行和单元格?
感谢您的任何反馈。
此致
当您收到 API 的响应时,您应该设置 self.users = arrayOfUsersYouReceivedFromServer
然后调用 self.tableView.reloadData()
。这个
填充users
后,调用tableView.reloadData()
。这将再次调用您的数据源方法。
完成获取用户后调用 tableView.reloadData()。