2009-12-15 71 views
1

經過了很長時間的閱讀網絡我決定來這裏,因爲我通常有我的問題回答超級快速和超級好堆棧溢出!添加一個UITextField到一個按鈕上的UITableView點擊

我試圖完成以下和將不勝感激的朝着正確的方向有任何建議或指點:當按下「+」按鈕的UITextField添加到

  • 顯示一個UITableView
  • 一行UITableView
  • 用戶可以在此字段中鍵入一個字符串
  • 如果用戶再次選擇「+」按鈕,則將另一個UITextfield添加到下一行。 (你的想法)
  • 當用戶完成添加UITextFields並填充它們時,他們可以點擊一個「保存」按鈕,此時每行中的所有條目都保存到一個數組中。

這方面的任何建議,將不勝感激

謝謝!

湯姆

回答

1

這個僞可能是一個起點:

// define an NSMutableArray called fields 

- (IBAction)addField:(id)sender { 
    // create a new text field here and add it to the array 
    // then reload data 
} 

- (IBAction)save:(id)sender { 
    for(UITextField *textField in fields) { 
     // whatever you have to do 
    } 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    // you have as many rows as textfields 
    return [fields count]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
    } 
    // Set up the cell... 
    // For row N, you add the textfield N to the array. 
    [cell addSubview:(UITextField *)[fields objectAtIndex:indexPath.row]]; 
} 
+0

謝謝你們..我知道這是一個有點含糊......但他真的幫了,我也對我的方式。謝謝 –

+0

我覺得你更願意在'if(cell == nil)'塊中調用'cell addSubview',所以每次單元被重用時都不會添加它。 – drewish

相關問題