2009-12-15 72 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

相关问题