2013-03-13 150 views
1

我正在做一個項目,其中我有一個TableView,其中有不同的行/單元格,並且在每個單元格中都有一個添加按鈕。我需要做的是點擊添加按鈕,新行應該添加在我點擊按鈕的行下方,這意味着我必須將一個單元格添加到該行上所選按鈕的下一個。並且用戶也可以輸入新行上的文字。請告訴我如何做到這一點。因爲我是iPhone開發新手。我會非常感謝....添加單元格的TableView

這裏是一個點擊按鈕功能,我要執行操作來添加新的單元格。

- (IBAction)AddButtonAction:(id)sender 
{ 
    [_choices insertObject:@"avav" atIndex:[_choices count]]; 
    [self.tableView reloadData]; 
    NSLog(@"here"); 

} 
+1

這將幫助你開始http://developer.apple.com/library/ios/#documentation/userexperience/conceptual/tableview_iphone/ManageInsertDeleteRow/ManageInsertDeleteRow.html – Chandu 2013-03-13 12:52:01

+0

我已經看到,我無法將它集成到我的項目 – Priyanka 2013-03-13 12:52:56

+0

你有在所有單元格textfeild? – Durgaprasad 2013-03-13 13:01:22

回答

2

使用此委託方法在選定行下添加新行。並使用自定義單元格對它有一個文本字段...

 rowCount ++;//Here rowcount refers the no. of rows in the table. 
    selectedIndexPath = indexPath;//Assign the selected indexpath for creating custom cell on it. 
    [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationBottom];//Indexpath refers the currently selected/targeted cell. 

在使用的cellForRowAtIndexPath這樣

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if((selectedIndexPath) && (indexPath.row == selectedIndexPath.row) && (indexPath.section == selectedIndexPath.section)) 
    { 
     static NSString *[email protected]"cell"; 
     NewCell *cell = (NewCell *) [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 

     if (cell == nil) 
     { 
      NSString *customeCellName = [NSString stringWithFormat:@"%@",[NewCell class]]; 

      NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:customeCellName owner:self options:nil]; 

      for (id currentObject in topLevelObjects) 
      { 
       if ([currentObject isKindOfClass:[UITableViewCell class]]) 
       { 
        cell = (NewCell *) currentObject; 
        break; 
       } 
      } 
     } 
     [cell setSelectionStyle:UITableViewCellSelectionStyleNone]; 


     return cell; 
    } 
    else 
    { 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 
    cell.textLabel.text = [NSString stringWithFormat:@"%d",indexPath.row]; 

    // Configure the cell... 

    return cell; 
    } 
} 

輸出會是這樣 enter image description here

需要定製按您的需求。

+0

不錯的答案.. + 1 – Dilip 2013-03-13 13:06:56

+0

@durgaprasad不,我有添加按鈕,我可以添加按鈕點擊行,並在新的行用戶可以輸入文本。 – Priyanka 2013-03-14 05:00:49

+0

你用這段代碼試過了嗎? – Ganapathy 2013-03-14 05:05:31

0

我認爲你在所有單元格中都有textfeild。在AddButtonAction函數中保存行的索引。在創建單元格時,會選擇單元格textfeild firstresponder。一旦用戶輸入數據,將其保存在數組中並重新加載。只需正確跟蹤selectedIndex。 我覺得這會有所幫助。

0

如果通過addSubview方法添加按鈕,電池,只需你可以用下面的代碼獲得indexPath:

- (IBAction)AddButtonAction:(id)sender 
{ 
    UIButton *button = (UIButton *)sender; 
    UITableViewCell *cell = (UITableViewCell *)[button superview]; 
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell]; 

    // Adding methods comes here... 
} 

如果您使用的是一些addSubview水平,你應該使用相同的superview方法逆訪問的UITableViewCell水平。如果使用自定義單元格,請用上述代碼中的UITableViewCell替換您的自定義單元類名稱。

要添加單元格,請查看WWDC 2011 Videos,「UITableView更改,提示&技巧」視頻。它顯示了一些好的和有用的方法來插入,刪除和重新加載其他行/部分之間的新行/部分,如insertRowsAtIndexPaths:withRowAnimation:方法。或見Apple Documents

+0

thankew so much much @Amir Kh – Priyanka 2013-07-05 10:41:11

相關問題