Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UITableView numberOfRows_Ios_Swift_Uitableview - Fatal编程技术网

Ios UITableView numberOfRows

Ios 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() }

我想用服务器数据更新tableview

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:
加载数据永远不会起作用。对每个单元格调用此方法,这意味着:

  • 如果没有单元格(数据尚未加载),则永远不会调用该方法,也不会加载您的数据(现在就是这样)

  • 如果加载了数据,则会为每个显示的单元格触发新的加载,也就是说,如果有5项,则会触发5次加载。这是递归的,因为
    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:
    加载数据永远不会工作。对每个单元格调用此方法,这意味着:

  • 如果没有单元格(数据尚未加载),则永远不会调用该方法,也不会加载您的数据(现在就是这样)

  • 如果加载了数据,则会为每个显示的单元格触发新的加载,也就是说,如果有5项,则会触发5次加载。这是递归的,因为
    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:
    加载数据永远不会工作。对每个单元格调用此方法,这意味着:

  • 如果没有单元格(数据尚未加载),则永远不会调用该方法,也不会加载您的数据(现在就是这样)

  • 如果加载了数据,则会为每个显示的单元格触发新的加载,也就是说,如果有5项,则会触发5次加载。这是递归的,因为
    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:
    加载数据永远不会工作。对每个单元格调用此方法,这意味着:

  • 如果没有单元格(数据尚未加载),则永远不会调用该方法,也不会加载您的数据(现在就是这样)

  • 如果加载了数据,则会为每个显示的单元格触发新的加载,也就是说,如果有5项,则会触发5次加载。这是递归的,因为
    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,非常有帮助!