开发者

How to use reloadData method of tableView iphone

I have one UITableViewController class. I am using NSMutableArray to fill table cells. I am fetching data from data开发者_StackOverflowbse & adding it to array. But cell is showing old results. How can I use reladData method & where should I call this method in my UITableViewController?


I quote Apple's documentation when to not use it:

The table view's delegate or data source calls this method when it wants the table view to completely reload its data. It should not be called in the methods that insert or delete rows, especially within an animation block implemented with calls to beginUpdates and endUpdates.

So call it when you changed your model but not using the tableview methods to alter the table like:

deleteRowsAtIndexPaths:withRowAnimation:
deleteSections:withRowAnimation:
insertRowsAtIndexPaths:withRowAnimation:
insertSections:withRowAnimation:


you can reload your table cell using

//fetch data from database.. and update array

//reload table data asper new data

[self.tableView reloadData];


You can (and should) call reloadData: when you want to force the table to be reloaded or when you know it hasn't been drawn yet.

Often, you will do this in your view controller's viewDidLoad: method or viewDidAppear: (or even viewWillAppear:).

Outside of these methods, if you substantially change the contents of the array of data being used to supply data to the table, you'd want to call reloadData:. For example, say you had a list of products with inventory levels for each. On initial view, you might just show everything in the array, in the table. But say you then have a filter applied to show only the products with zero inventory. The array with the products would be reduced to include only those products, so after reducing the array you'd call reloadData: on the table so that the view was updated.

And by the way, as I discovered recently, when you call reloadData:, the table is reloaded asynchronously (and pretty quickly). However, the methods deleteRows..., deleteSections..., insertRows..., and insertSections... (especially between beginUpdates: and endUpdates: calls) are executed synchronously, and can, for large tables, slow your app down (and freeze the UI) considerably. Your mileage may vary, but that was my experience with these methods. I actually went to a model where I use deleteSections... and then use reloadData: (because I was deleting all the sections anyway).


Whenever you call reloadData method, the table view will be refreshed. As you want to refresh the table after loading data from database, you can call reload data when the fetching is done.

// code to fetch data from database

// when done, reload the table
[myTableView reloadData];


Add { didSet { tableview.reloadData() } in front of your instance class of type array.

Example:

Swift class code:

class Data {
  var name  = ""
  var height = ""
}

ViewController code / Instant DataClass of type empty array:

let dataINDataClass = [Data]  { 

  didSet {

  tableview.reloadData()

}

Hope this helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜