I want to change the number of tableviews depending on my input, which will change depending on the user and i need to be able to reference these tableviews in my data source, as they should retain different data depending on the tableview. I figured this might be done if I could create a unique variable for each of the created tableviews, but i would need to be able to reference these variables through my whole class?
-
4And what's the question?user529758– user5297582012-06-22 02:25:19 +00:00Commented Jun 22, 2012 at 2:25
-
1my questions was if it was possible to create variables which i could reference through my whole class programmatically.Niels Sønderbæk– Niels Sønderbæk2012-06-22 02:39:28 +00:00Commented Jun 22, 2012 at 2:39
2 Answers
You can create an NSMutable Array and add UITableView in them directly . You can make create global variable store using static reference and access it globally .
@interface Singleton : NSObject {
NSMutableArray *TableArray;
}
+ (Singleton *)instance;
@end
@implementation Singleton
+ (Singleton *)instance {
static Singleton *instance;
@synchronized(self) {
if(!instance) {
instance = [[Singleton alloc] init];
}
}
return instance;
}
The other alternative would be to include NSMutableArray in you appdelegate and access it throughout your application globally.
Creating objects dynamically is not a problem . you can create as many UITableview's and add them to an NSMutableArray using.
UITableview *temp = [UITableview alloc] initWithFrame : ... ];
temp.delegate = self ;
tableArray.addObject(temp);
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath(NSIndexPath *)indexPath{
if(tableView = [tableArray objectAtIndex:x]{
// do this
}
else if (select appropriate table view from array ){
}
//do this for the rest
}
What you are facing is an design issue . For other alternatives please refer Apple Guides for Objects Communication
Comments
Why not create an NSMutableArray member variable to store them all? When you need to change the data source, set the specific tableview to the data source. I'm not sure of the details of what exactly you're trying to do, but adding them dynamically and changing the data source shouldn't be a problem.