Ios UITableView numberOfRows
我想用服务器数据更新tableviewIos UITableView numberOfRows,ios,swift,uitableview,Ios,Swift,Uitableview,我想用服务器数据更新tableview class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { @IBOutlet weak var tableView: UITableView! var aedList = [AED]() override func viewDidLoad() { super.viewDidLoad() }
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var tableView: UITableView!
var aedList = [AED]()
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewWillAppear(animated: Bool) {
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.aedList.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cellIdentifier = "Cell"
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath)
Server().getJsonInformationFromServer(url: "aeds") { (response) -> Void in
self.aedList = JSONHelper().parseAEDInformation(json: response["data"])
dispatch_async(dispatch_get_main_queue(), { () -> Void in
cell.textLabel?.text = self.aedList[indexPath.row].street
self.tableView.reloadData()
})
}
return cell
}
数据正在正确下载,但tableview为空。
我想这是由于我的下载的异步行为和numberOfRows
方法试图计算一个仍然为空的数组
如何更正此错误以正确显示数据?我也需要在那里使用异步调度吗?从
tableView:cellforrowatinexpath:
加载数据永远不会起作用。对每个单元格调用此方法,这意味着:
reloadData
将重新显示所有单元格override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
Server().getJsonInformationFromServer(url: "aeds") { (response) -> Void in
self.aedList = JSONHelper().parseAEDInformation(json: response["data"])
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.tableView.reloadData()
})
}
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cellIdentifier = "Cell"
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath)
cell.textLabel?.text = self.aedList[indexPath.row].street
return cell
}
从
tableView:cellforrowatinexpath:
加载数据永远不会工作。对每个单元格调用此方法,这意味着:
reloadData
将重新显示所有单元格override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
Server().getJsonInformationFromServer(url: "aeds") { (response) -> Void in
self.aedList = JSONHelper().parseAEDInformation(json: response["data"])
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.tableView.reloadData()
})
}
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cellIdentifier = "Cell"
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath)
cell.textLabel?.text = self.aedList[indexPath.row].street
return cell
}
从
tableView:cellforrowatinexpath:
加载数据永远不会工作。对每个单元格调用此方法,这意味着:
reloadData
将重新显示所有单元格override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
Server().getJsonInformationFromServer(url: "aeds") { (response) -> Void in
self.aedList = JSONHelper().parseAEDInformation(json: response["data"])
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.tableView.reloadData()
})
}
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cellIdentifier = "Cell"
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath)
cell.textLabel?.text = self.aedList[indexPath.row].street
return cell
}
从
tableView:cellforrowatinexpath:
加载数据永远不会工作。对每个单元格调用此方法,这意味着:
reloadData
将重新显示所有单元格override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
Server().getJsonInformationFromServer(url: "aeds") { (response) -> Void in
self.aedList = JSONHelper().parseAEDInformation(json: response["data"])
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.tableView.reloadData()
})
}
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cellIdentifier = "Cell"
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath)
cell.textLabel?.text = self.aedList[indexPath.row].street
return cell
}
cellForRowAtIndexPath正如方法名称所示,它由每个单元格的表视图调用,因此您不应该在那里获取整个数据,它用于单独填充单元格。以下是代码中的几个修复程序:
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var tableView: UITableView!
var aedList = [AED]()
//In addition to viewDidLoad, you can call this method whenever you wanna update your table
func fetchData() {
Server().getJsonInformationFromServer(url: "aeds") { (response) -> Void in
self.aedList = JSONHelper().parseAEDInformation(json: response["data"])
self.aedList = JSONHelper().parseAEDInformation(json: response["data"])
dispatch_async(dispatch_get_main_queue()) { [weak self] in
self?.tableView.reloadData()
}
})
}
override func viewDidLoad() {
super.viewDidLoad()
fetchData()
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return aedList.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cellIdentifier = "Cell"
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath)
cell.textLabel?.text = self.aedList[indexPath.row].street
return cell
}
cellForRowAtIndexPath正如方法名称所示,它由每个单元格的表视图调用,因此您不应该在那里获取整个数据,它用于单独填充单元格。以下是代码中的几个修复程序:
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var tableView: UITableView!
var aedList = [AED]()
//In addition to viewDidLoad, you can call this method whenever you wanna update your table
func fetchData() {
Server().getJsonInformationFromServer(url: "aeds") { (response) -> Void in
self.aedList = JSONHelper().parseAEDInformation(json: response["data"])
self.aedList = JSONHelper().parseAEDInformation(json: response["data"])
dispatch_async(dispatch_get_main_queue()) { [weak self] in
self?.tableView.reloadData()
}
})
}
override func viewDidLoad() {
super.viewDidLoad()
fetchData()
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return aedList.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cellIdentifier = "Cell"
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath)
cell.textLabel?.text = self.aedList[indexPath.row].street
return cell
}
cellForRowAtIndexPath正如方法名称所示,它由每个单元格的表视图调用,因此您不应该在那里获取整个数据,它用于单独填充单元格。以下是代码中的几个修复程序:
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var tableView: UITableView!
var aedList = [AED]()
//In addition to viewDidLoad, you can call this method whenever you wanna update your table
func fetchData() {
Server().getJsonInformationFromServer(url: "aeds") { (response) -> Void in
self.aedList = JSONHelper().parseAEDInformation(json: response["data"])
self.aedList = JSONHelper().parseAEDInformation(json: response["data"])
dispatch_async(dispatch_get_main_queue()) { [weak self] in
self?.tableView.reloadData()
}
})
}
override func viewDidLoad() {
super.viewDidLoad()
fetchData()
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return aedList.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cellIdentifier = "Cell"
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath)
cell.textLabel?.text = self.aedList[indexPath.row].street
return cell
}
cellForRowAtIndexPath正如方法名称所示,它由每个单元格的表视图调用,因此您不应该在那里获取整个数据,它用于单独填充单元格。以下是代码中的几个修复程序:
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var tableView: UITableView!
var aedList = [AED]()
//In addition to viewDidLoad, you can call this method whenever you wanna update your table
func fetchData() {
Server().getJsonInformationFromServer(url: "aeds") { (response) -> Void in
self.aedList = JSONHelper().parseAEDInformation(json: response["data"])
self.aedList = JSONHelper().parseAEDInformation(json: response["data"])
dispatch_async(dispatch_get_main_queue()) { [weak self] in
self?.tableView.reloadData()
}
})
}
override func viewDidLoad() {
super.viewDidLoad()
fetchData()
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return aedList.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cellIdentifier = "Cell"
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath)
cell.textLabel?.text = self.aedList[indexPath.row].street
return cell
}
谢谢你,卢卡斯!我完全忘记了这样一个事实:我正在为我的方法中的每个单元格从服务器加载内容!发生这种情况:),我很高兴我帮助了
numberOfSectionInTableView
是可选的,您不需要实现它。它默认返回1。我知道它是默认值,但不确定它是否返回1。谢谢,我会编辑:)谢谢你,卢卡斯!我完全忘记了这样一个事实:我正在为我的方法中的每个单元格从服务器加载内容!发生这种情况:),我很高兴我帮助了numberOfSectionInTableView
是可选的,您不需要实现它。它默认返回1。我知道它是默认值,但不确定它是否返回1。谢谢,我会编辑:)谢谢你,卢卡斯!我完全忘记了这样一个事实:我正在为我的方法中的每个单元格从服务器加载内容!发生这种情况:),我很高兴我帮助了numberOfSectionInTableView
是可选的,您不需要实现它。它默认返回1。我知道它是默认值,但不确定它是否返回1。谢谢,我会编辑:)谢谢你,卢卡斯!我完全忘记了这样一个事实:我正在为我的方法中的每个单元格从服务器加载内容!发生这种情况:),我很高兴我帮助了numberOfSectionInTableView
是可选的,您不需要实现它。它默认返回1。我知道它是默认值,但不确定它是否返回1。谢谢,我会编辑:)谢谢你,Sulthan,非常有帮助!谢谢Sulthan,非常有帮助!谢谢Sulthan,非常有帮助!谢谢Sulthan,非常有帮助!