2010-12-21 80 views

回答

1

如果要插入表格視圖單元,當你點擊一個細胞,嘗試下面的代碼。

讓我們考慮你要在第0部分的位置1,2和3中插入3行。在didSelectRowAtIndexPath方法中,執行以下操作。如果我們選擇第0節(第一節)中的第0行(第一行),這些行將被插入第0節。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

    if (indexPath.row == 0 && indexPath.section == 0) { 

     // Create indexPaths for the rows you are going to insert 

     NSIndexPath *indexPath1 = [NSIndexPath indexPathForRow:1 inSection:0]]; 
     NSIndexPath *indexPath2 = [NSIndexPath indexPathForRow:2 inSection:0]]; 
     NSIndexPath *indexPath3 = [NSIndexPath indexPathForRow:3 inSection:0]]; 

     // Increase the number of rows in section 0 by 3, as we are inserting 3 rows here 

     numberOfRowsInSectionZero = numberOfRowsInSectionZero + 3; 

     // Insert the rows 

     [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath1, indexPath2, indexPath3, nil] withRowAnimation:UITableViewRowAnimationFade]; 
    } 
} 

在你numberOfRowsInSection方法,你應該有類似下面的,

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section { 

    // some code here 

    if (section == 0) return numberOfRowsInSectionZero; 

    // some code here 
} 

並確保您有在的cellForRowAtIndexPath適當的代碼方法顯示在新顯示的內容行。

注意:您必須改變您的代碼,以便在單擊第0節的行0時插入的行未插入。

0

使didSelectRow一個布爾(isRowClick)變量和

集YES,然後在此線

[yourTable reloadData]; 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 

if(isRowClick) 
    return 7;//according to you 
else 
    return 2; 
} 



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

    isRowSelect=YES; 
//stuff 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

//stuff according to you 
} 

這將HEP你。

0
  1. 定義數據對象,例如, NSMutableDictionary
  2. 總是讀出這個對象的數據(對於count和你想要顯示的值)
  3. 在用戶點擊定義的單元格的情況下。您將刷新MutableDirectory並添加新數據。
  4. 重新加載TableViewController [tableviewController.tableview reloadData];

完蛋了 歡呼西蒙

相關問題