Reload Table View with different values iPhone

Sometimes, It is required to use same tableviewcontroller multiple times with different parameters. For example, in a game, we need to show same screen with different elements. Here, our object is to reuse UITableViewController with different parameters and it will be happened on navigation next button click.
Here are the simple steps to do this:

1. Declare all parameters which make changes in tableview in AppDelegate class.
2. Load all parameters/Data sources in applicationDidFinishLaunching method.
3. Add a UITableViewController class and add all required controls, methods and properties.
4. In init method of UITableViewController initialize appDelegate class object to access the parameters.
5. Now in following method loads control with the appDelegate's parameters value and add it in the cell view.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

...... Setup the cell ......

}


6. Process and Call [self loadView]; where you want to reload data with changes.

Suppose you need to set tableview according NSMutableArray and On next click, you need to show next element of NSMutableArray.
Then declare index parameter in AppDelegate class and use it in cellForRowAtIndexPath method. On next click, Increment the index value and reload using [self loadView]; It will call cellForRowAtIndexPath again for the next value.

Hope, It helps.

No comments:

Post a Comment