Iphone 单击按钮将UITextField添加到UITableView
在阅读了相当长的一段时间之后,我决定来这里,因为我的问题通常会得到超级快速和超级好的堆栈溢出回答 我正试图完成以下内容,如有任何正确方向的建议或指点,我将不胜感激:Iphone 单击按钮将UITextField添加到UITableView,iphone,uitableview,uitextfield,Iphone,Uitableview,Uitextfield,在阅读了相当长的一段时间之后,我决定来这里,因为我的问题通常会得到超级快速和超级好的堆栈溢出回答 我正试图完成以下内容,如有任何正确方向的建议或指点,我将不胜感激: 显示UITableView 当按下“+”按钮时,一个UITextField被添加到UITableView的一行中 用户可以在此字段中键入字符串 如果用户再次选择“+”按钮,另一个UITextfield将添加到下一行。(你明白了) 当用户添加完UITextFields并填充它们后,他们可以点击“保存”按钮,此时每行中的所有条目都保存
- 显示UITableView
- 当按下“+”按钮时,一个UITextField被添加到UITableView的一行中
- 用户可以在此字段中键入字符串
- 如果用户再次选择“+”按钮,另一个UITextfield将添加到下一行。(你明白了)
- 当用户添加完UITextFields并填充它们后,他们可以点击“保存”按钮,此时每行中的所有条目都保存到一个数组中
Tom此伪代码可能是一个起点:
// 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]];
}
此伪代码可能是一个起点:
// 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]];
}
SWIFT 4的更新 您可以在此处使用beginUpdate和endUpdate,而不是重新加载整个表 将目标添加到单元格按钮(可以使用视图标记或创建新的TableviewCell类) 然后执行此方法:
@objc func addTextField(_ sender:UIButton) {
let indexPath = IndexPath(row: sender.tag, section: 0)
if let cell = tblView.cellForRow(at: indexPath) as? TableCell {
tblArray.append(cell.txtField.text!)
}
let newRow = IndexPath(row: sender.tag + 1, section: 0)
tblView.beginUpdates()
tblView.insertRows(at: [newRow], with: .left)
tblView.endUpdates()
}
不要忘记将var tblArray:[Any]=[“”]
放在开头
希望这是任何人谁是寻找迅速的答案
快乐编码。SWIFT 4的更新 您可以在此处使用beginUpdate和endUpdate,而不是重新加载整个表 将目标添加到单元格按钮(可以使用视图标记或创建新的TableviewCell类) 然后执行此方法:
@objc func addTextField(_ sender:UIButton) {
let indexPath = IndexPath(row: sender.tag, section: 0)
if let cell = tblView.cellForRow(at: indexPath) as? TableCell {
tblArray.append(cell.txtField.text!)
}
let newRow = IndexPath(row: sender.tag + 1, section: 0)
tblView.beginUpdates()
tblView.insertRows(at: [newRow], with: .left)
tblView.endUpdates()
}
不要忘记将var tblArray:[Any]=[“”]
放在开头
希望这是任何人谁是寻找迅速的答案
快乐的编码。谢谢各位。。我知道这有点模糊。。。但他的努力确实起到了帮助,我也在路上。谢谢,我觉得你更愿意在
if(cell==nil)
块中调用cell addSubview
,这样你就不会在每次重复使用该单元格时都添加它了。谢谢各位。。我知道这有点模糊。。。但他的努力确实起到了帮助,我也在路上。谢谢,我觉得您更愿意在if(cell==nil)
块中调用cell addSubview
,这样您就不会每次重复使用单元格时都添加它了。