Reputation: 734
I'm adding multiple tableviews into the scrollview based on the array count...and the code is here
int currentX = 3;
for(int i=0;i<[restNameArray count]; i++)
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectMake(currentX, 0, 150, 35)];
button.tag = i+1;
//[button setUserInteractionEnabled:NO];
[button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[button setBackgroundImage:[UIImage imageNamed:@"ohris_applytics_ipad_middle_oc.png"] forState:UIControlStateNormal];
button.titleLabel.font = [UIFont fontWithName:@"HelveticaNeue-CondensedBold" size:15.0f];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[button setTitle:[NSString stringWithFormat:@"%@", [restNameArray objectAtIndex:i]] forState:UIControlStateNormal];
restaurantTable = [[UITableView alloc] initWithFrame:CGRectMake(currentX, 35, 150, 310) style:UITableViewStylePlain];
[restaurantTable setTag:i+1];
[restaurantTable setBackgroundColor:[UIColor whiteColor]];
restaurantTable.delegate = self;
restaurantTable.dataSource = self;
[scrollView addSubview:button];
[scrollView addSubview:restaurantTable];
NSLog(@"tag %d",restaurantTable.tag);
currentX = restaurantTable.frame.size.width+currentX+3;
[tagArray addObject:[NSString stringWithFormat:@"%d", restaurantTable.tag]];
}
NSLog(@"aaaa %@", tagArray);
[scrollView setContentSize:CGSizeMake(currentX, 349)];
It is showing fine in the simulator. And i need to populate the tableviews with array. And the code at cellforrowatindexpath is
if(tableView == restaurantTable){
for (int i =0;i<[tagArray count];i++)
{
table1Array = [[[restSalesArray objectAtIndex:i]componentsSeparatedByString:@","]mutableCopy];
NSLog(@"Sales array :%d %@", i, table1Array);
cell.textLabel.text = [formatter stringFromNumber:[NSNumber numberWithInteger:[[table1Array objectAtIndex:indexPath.row]intValue]]];
cell.textLabel.textAlignment = NSTextAlignmentCenter;
}
}
But the problems is, it is populating only in the last tableview.. remaining are empty... plz help me out guys....
Upvotes: 2
Views: 1112
Reputation: 1993
You have to first assign tag for each tableView. Then you can get selected row of particular table by following code:-
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
int tableTag = tableView.tag;
if(tableTag==0)
{
//do something here
}
else if(tableTag==1)
{
//do something here
}
}
Upvotes: 0
Reputation: 6065
It is ok what you are trying to do. But you are overriding the restaurantTable
variable in your for loop. So the variable is the last table view. You need to add them to an array or seperate variables to be able to compare with if in you cellForRowAtIndexPath
method.
_allTableViews is defined in your header file
you for loop
_allTableViews = [NSMutableArray array];
for (.....) {
.
.
.
UITableView *tableView = [[UITableView alloc] initWith....];
.
.
.
[_allTableViews addObject tableView];
}
cellForRowAtIndePath
NSInteger tableIndex = [_allTableViews indexOfObject:tableView];
// Now you have the index of table view
switch (tableIndex) {
case 0:
// first table
break;
case 1:
// second table
break;
default:
break;
}
Upvotes: 3
Reputation: 46533
You need to compare each tableView either by its outlet name or tag.
As :
if(tableView == _restaurantTableOutlet){
...
}
else if(tableView == _barTableOutlet){
...
}
else if(tableView == _loungeTableOutlet){
...
}
Upvotes: 2
Reputation: 3937
You are storing each table view into restaurantTable
, so the references to previous table views are lost. Either store them all in an array, or don't store any of them
Why are you checking if(tableView == restaurantTable)
? You should check the tables by tag and work with each one separately
Upvotes: 0