-2

I have a scroll view, and inside the scroll view I programmatically add tables with same name (the tables count not always the same number) and I add them in the scroll using a loop.

My issue: my app makes the first table, puts data in it, then builds the second one and puts data, but when it puts data in the second one, the data in the first one changes because they have the same name.

How can i fix this issue?

for ( int i=0; i<[P1Rows count]; i++) {
    [self AddPricesTable:i];
}


- (void)AddPricesTable:(int)GroupNum{
    self.TableView = [[[UITableView alloc] initWithFrame:CGRectMake(ScrollView.frame.size.width * GroupNum, 0,
                    ScrollView.frame.size.width, ScrollView.frame.size.height) style:UITableViewStylePlain] autorelease];
    self.TableView.backgroundColor = [UIColor blackColor];
    self.TableView.dataSource = self;
    self.TableView.delegate = self;

    P1Dict = [P1Rows objectAtIndex: GroupNum];
    NSLog(@"%@", [P1Dict objectForKey:@"Group"]);
    P2URL = [NSURL URLWithString:[NSString stringWithFormat:@"http://xxx.co/SFP/?Q=P2&V=%@",
            [[P1Dict objectForKey:@"Group"] stringByReplacingOccurrencesOfString:@" " withString:@"%20"]]];
    NSLog(@"%@", P2URL);
    P2JsonString = [[NSString alloc] initWithContentsOfURL:P2URL usedEncoding:Encoding error:&Error];
    P2JsonData = [P2JsonString dataUsingEncoding:NSUTF32BigEndianStringEncoding];
    P2Dict = [[[CJSONDeserializer deserializer] deserializeAsDictionary:P2JsonData error:&Error] retain];
    [self CheckConnect];
    P2Rows = [P2Dict objectForKey:@"SFP"];

    [self.TableView reloadData];

    [ScrollView addSubview:self.TableView];
}
  • 1
    Really need some source code to see what you are talking about. If the tables have the same name then shouldn't it show the same thing?? Also, are you pointing both to the same data-source? If so... you'll expect them to have the same items... yes? – dineth Aug 12 '12 at 11:00
  • Can you add a more thorough explanation. Try explaining what you're trying to achieve, what you've done so far (with sample code), and what's not working? – jstr Aug 12 '12 at 11:01
  • how could 2 different items have the same name? – John Smith Aug 12 '12 at 11:02
  • 1
    ok, can you tell me how to use dynamic var names to fix this issue. please please please – user1526898 Aug 12 '12 at 11:02
  • i used the same name because i havent find how to use dynamic var names – user1526898 Aug 12 '12 at 11:03
  • dynamic var name change var name from the code – user1526898 Aug 12 '12 at 11:04
  • `for(i=0: i=6; i++){ new[i] = i }` the vars name i want it ` new0 new1 new2 new3 new4 new5 ` – user1526898 Aug 12 '12 at 11:05
  • 2
    so confused. pls just post some of your source code if you want an answer... – dineth Aug 12 '12 at 11:06
  • choose the var name from the code – user1526898 Aug 12 '12 at 11:07
  • 1
    as long as you are not saying what do you wanna achieve, it will look like you have taken the wrong approach – John Smith Aug 12 '12 at 11:09
  • 1
    why, the heck, do you need to choose the var name? – John Smith Aug 12 '12 at 11:09
  • 1
    Look, dude, there are two situations when people ask for help here: FIRST: the have a task, they tried to do it with a good approach, but something went wrong with coding. SECOND: they have taken the wrong approach at all, and in this case they have to post the task, not the code itself. You are in the second situation. – John Smith Aug 12 '12 at 11:13
  • I agree with @MahmoudFayez there are memory leaks here. But it can be done. I do this in one of my apps. Unfortunately I can't remember of the top of my head. If you still don't have an answer tomorrow I will add. – Popeye Aug 12 '12 at 11:47
  • possible duplicate: [Create multiple variables based on an int count](http://stackoverflow.com/questions/2231783/create-multiple-variables-based-on-an-int-count/) – jscs Aug 12 '12 at 17:24

2 Answers2

1

You are obviously` leaking memory here. Also you do not need to have multiple tables just one table is enough and add it in the Interface builder.

All you need is to set the sections in the UITabelView to the number of tables you are using right now.

Mahmoud Fayez
  • 3,398
  • 2
  • 19
  • 36
0

You have to declare 2 UITableView variables, make them outlets to your 2 tables, hook them in interface builder, and give the delegates and datasources.

Then in your cellForRowAtIndexPath: say:

if (tableView == outlet_to_table_ONE){
    //populate the FIRST table as you wish
}
else{
    //else should mean that tableView will be outlet_to_table_TWO (if you have only 2 tables)
    //populate the SECOND table as you wish
}
John Smith
  • 2,012
  • 1
  • 21
  • 33
  • He have more than 2 tables. So he tries to make it dynamic. – Mahmoud Fayez Aug 12 '12 at 11:54
  • yes my tables not always the same number – user1526898 Aug 12 '12 at 12:00
  • Then, if you place tables dynamically, all of them have a parameter: tableView.tag. When you place them, set their tag by an incremented integer, and in your cellForRowAtIndexPath, check them by tag: if tableView.tag == 0, do this, else if tableView.tag == 1 , do that, and so on. – John Smith Aug 12 '12 at 12:15