UITableView.reloadData() 无法获取正确的文本 [英] UITableView.reloadData() cannot get retrieve correct text

查看:24
本文介绍了UITableView.reloadData() 无法获取正确的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个获取动态值的 UITableView.这些值取决于用户输入.用户输入一个字符串,代码使用该字符串从 Internet 获取一个数组.在我看来,UITableView 和 textField 正在工作.当我按下 Get Tower(getTowerButton()) 按钮时,UITableView 应该用 reloadData() 重新加载它的数据.但它不是重新加载,因为它不打印 1 和 2.

I've a UITableView that gets dynamic values. These values depends on user entry. User enters a string and the code gets an array from internet by using that string. In my view, UITableView and textField is working. When I press Get Tower(getTowerButton()) button, UITableView should reload its data with reloadData(). But it's not reloading, because it doesn't print 1 and 2.

注意:如果我将 UITableView 和用户条目分开到两个不同的视图代码中.和表加载.

Note: If I seperate UITableView and user entry to two different views code works. and table loads.

class AddTowerViewController: UIViewController, UITextFieldDelegate, UITableViewDelegate, UITableViewDataSource {
    
    
    @IBOutlet weak var ICAOCodeField: UITextField!
    @IBOutlet weak var towersTable: UITableView!
    var towersArray: Array<String> = []
    override func viewDidLoad() {
        super.viewDidLoad()
        self.ICAOCodeField.delegate = self
        
                // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    

    @IBAction func getTowers() {
        towersArray = getTowerNames(ICAOCode: ICAOCodeField.text!)
        self.towersTable.reloadData()
    }
    
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = towersTable.dequeueReusableCellWithIdentifier("Cellos", forIndexPath: indexPath) as UITableViewCell
        cell.textLabel?.text = towersArray[indexPath.row]
        return cell
    }
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return towersArray.count
    }
...
       

我注意到当我将 UITableView 和用户条目视图分开时,表格会加载数据.我猜这个问题是因为系统只在第一次启动所属视图时运行 numberOfRowsInSection 和 cellForRowAtIndexPath 方法.

I've noticed that when I seperate UITableView and user entry view the table loads the data. I guess that the problem causes because system runs numberOfRowsInSection and cellForRowAtIndexPath methods only at first launch of belonging view.

推荐答案

您是说 getTowerNames(ICAOCode: ICAOCodeField.text!) 从 Internet 加载数据,但我认为您并没有同步执行此操作.我建议您创建一个完成块来处理请求的响应.像这样:

You're saying that getTowerNames(ICAOCode: ICAOCodeField.text!) loads data from the internet and I don't think you are doing that synchonously. I suggest you to create a completion block to handle the request's response. Like this:

getTowerNames(ICAOCode: ICAOCodeField.text!, completion:(names: Array<String>) -> ())

然后使用完成闭包重新加载您的表.

Then use the completion closure to reload your table.

getTowerNames(ICAOCode: ICAOCodeField.text!) {[weak self] names in 
    self?.towerNameArray = names
    self?.tableView.reloadData() 
}

希望有所帮助.

这篇关于UITableView.reloadData() 无法获取正确的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆