Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reload data in programmatically created tableView

I have looked at a number of other SO questions related to this question and have not found one dealing with my specific situation. I create a variable number of tableViews programmatically in one method. At a later point, I would like to reload the data in any one of the tableViews in another method. I would like to use...

[tableView reloadData];

to reload the data in a specific tableView. The problem I am having is accessing the tableView that was created programmatically at a later point. My thought is to set a unique tag for each tableView as it is created.

So here is my question. Is it possible to create and initialize a tableView by referring to an existing tableView's tag. For instance, with a UIView it is possible to...

UIView *notATableView = [[UIView alloc] viewWithTag:1];

which would allow me to modify the view whose tag was equal to 1. Does a similar possibility exist for tableViews?

like image 847
Andrew Hart Avatar asked Aug 06 '13 19:08

Andrew Hart


2 Answers

Just refer to the UITableViews by their property names.

[self.thisTableView reloadData];
[self.thatTableView reloadData];

No tags needed!

You can also use the property names for IF checking in your UITableViewDataSource and UITableViewDelegate methods

if(tableView == self.thisTableView){
    [tableView reloadData];
}
like image 196
hgwhittle Avatar answered Oct 13 '22 11:10

hgwhittle


Yes, you can set the tag on your table view when you create it and retrieve the table view later using viewWithTag.

// Assuming you've added the table view as a subview to the current view controller
UITableView *tableView = (UITableView *)[self.view viewWithTag:1];

However, you're not re-allocating it. You're just getting a pointer to it.

Then, just reload the data or do whatever:

[tableView reloadData];
like image 41
Marcus Adams Avatar answered Oct 13 '22 11:10

Marcus Adams