2012-08-12 46 views
-2

我有一個滾動視圖,並且在滾動視圖中,我以編程方式添加具有相同名稱的表格(表格不總是相同的數字),並使用循環將其添加到滾動中。iPhone:以編程方式添加具有相同名稱的表格

我的問題:我的應用程序創建第一個表,將數據放入其中,然後構建第二個表並放入數據,但是當它將數據放入第二個表中時,第一個數據會因爲它們具有相同名稱。

我該如何解決這個問題?

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

真的需要一些源代碼,看看你在說什麼。如果表具有相同的名稱,那麼它不應該顯示相同的東西?另外,你是否指向同一個數據源?如果是這樣......你會期望他們擁有相同的物品......是的? – dineth 2012-08-12 11:00:37

+0

您可以添加更詳盡的解釋。嘗試解釋你想要達到的目標,到目前爲止你已經做了什麼(使用示例代碼),哪些不起作用? – jstr 2012-08-12 11:01:28

+0

2個不同的物品如何能有相同的名稱? – 2012-08-12 11:02:03

回答

1

你很明顯是在這裏泄漏記憶。你也不需要有多個表,只需要一張表就足夠了,並將其添加到界面構建器中。

您只需要將UITabelView中的部分設置爲您正在使用的表的數量即可。

0

你必須聲明2個UITableView變量,使它們出口到你的2個表,將它們掛接到接口構建器中,並給出委託和數據源。

然後在您的cellForRowAtIndexPath:說:

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 
} 
+0

他有2個以上表。所以他試圖讓它變得動態。 – 2012-08-12 11:54:41

+0

是的,我的表格不總是相同的數字 – user1526898 2012-08-12 12:00:50

+0

然後,如果你動態地放置表格,它們都有一個參數:tableView.tag。放置它們時,按遞增的整數設置它們的標記,並在cellForRowAtIndexPath中通過標記檢查它們:if tableView.tag == 0,執行此操作,否則,如果tableView.tag == 1,則執行此操作,依此類推。 – 2012-08-12 12:15:53