reloadData() isn't work in swift

2019-09-09 22:57发布

So, I have trouble - can't reloadData of TableView, in command line I see good data - when I click button idMeet is changed, but in Simulator I don't see refresh table view after click on the button, and idMeet is changed and my query is changed too. I understand that I must use tableView.reloadData(), but don't understand where write it for correct working?

    var x = true;

        @IBOutlet weak var myButton: UIButton!
        @IBOutlet var tbRouteData: UITableView!
        @IBAction func act(sender: UIButton) {
            x = !x
            getInfo()
        }

        override func viewDidLoad() {
            super.viewDidLoad()
        }

        override func viewWillAppear(animated: Bool) {
            self.getTmp()
        }

        func getInfo() -> NSMutableArray {
            sharedInstance.database!.open()
         var idMeet: Int
         if x {
                idMeet = 1
            } else {
                idMeet = 2
            }

            print(idroute)

            let selectInfo: FMResultSet! = sharedInstance.database!.executeQuery("SELECT * FROM t1 AS one JOIN t2 AS two ON (one.id = two.id AND two.line_id = ?) ORDER BY two.position ASC", withArgumentsInArray: [idMeet])
    ...
            return tmp
        }

    func getTmp(){

        infoTmp = NSMutableArray()
        infoTmp = getInfo()
    }


    // MARK: - Table view data source

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return infoTmp.count
    }

 override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! PrivateCell

        let my:Info = infoTmp.objectAtIndex(indexPath.row) as! Info
        cell.listOfStop.text = my.Name.uppercaseString
        return cell
    }

4条回答
等我变得足够好
2楼-- · 2019-09-09 23:15

The reload data should be completed within the button but after the getInfo() has been completed. The following should work:

    @IBAction func act(sender: UIButton) {
        x = !x
        getInfo()
        tbRouteData.reloadData()
    }
查看更多
Root(大扎)
3楼-- · 2019-09-09 23:28

There are a few issues here:

  1. The getTmp function should reload the data:

    func getTmp(){
        infoTmp = getInfo()
        tbRouteData.reloadData()
    }
    
  2. Now that you have a function that populates the property and reloads the table, act should call that, rather than getInfo:

    @IBAction func act(sender: UIButton) {
        x = !x
        getTmp()
    }
    

    When you call getInfo, that doesn't set infoTmp. And by calling getTmp that centralizes the population of infoTmp and reloading of the table in one place.

  3. If you're not seeing any data in your table view, make sure that you've set the delegate of the table view accordingly. You can do that either in IB, or programmatically:

    override func viewDidLoad() {
        super.viewDidLoad()
        tbRouteData.delegate = self
    }
    

    This assumes, of course, that you defined your view controller to conform to UITableViewDataSource and have implemented the appropriate methods. Without seeing those methods, we can't tell if there might be some further issue there.

查看更多
我命由我不由天
4楼-- · 2019-09-09 23:28

Usually when tableView data array become changed then tableView should reload. In this case at the end of the button action method reload tableview.

@IBAction func act(sender: UIButton) {
        x = !x
        getInfo()
        tbRouteData.reloadData()
}
查看更多
smile是对你的礼貌
5楼-- · 2019-09-09 23:32
func getTmp(){

        infoTmp = NSMutableArray()
        infoTmp = getInfo()
        tbRouteData.reloadData()
    }

Write this code and this will reload your tableview with new data after the array fills up

查看更多
登录 后发表回答