如何添加多个视图和删除视图并在删除后从视图中获取所有数据?

Posted

技术标签:

【中文标题】如何添加多个视图和删除视图并在删除后从视图中获取所有数据?【英文标题】:How to add multipile views and delete view and get all the data from the the views after deleting? 【发布时间】:2017-01-12 10:31:13 【问题描述】:

我想用一个文本字段创建多个视图,现在我必须在单击 + 按钮时创建多个视图,如果单击 - 按钮,则视图必须删除。我确实使用了这段代码。

@interface ViewController ()

IBOutlet UITableView *viewsTbl;
IBOutlet UIButton *addBtn;
NSMutableArray *countArr;
NSMutableDictionary *dataArr;
NSInteger number;


@end

@implementation ViewController

- (void)viewDidLoad 
[super viewDidLoad];
countArr = [NSMutableArray array];
dataArr = [NSMutableDictionary dictionary];
[dataArr setValue:@"" forKey:@"TextFieldOne"];
[dataArr setValue:@"" forKey:@"TextFieldTwo"];
[countArr addObject:dataArr];


// Do any additional setup after loading the view, typically from a nib.


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

   return  countArr.count;

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

ViewTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"ViewTableViewCell"];
if (cell == nil) 
    cell = [[[NSBundle mainBundle] loadNibNamed:@"ViewTableViewCell" owner:self options:nil] objectAtIndex:0];

if (countArr.count == indexPath.row+1)

    cell.buttonPlus.hidden = NO;

else

    cell.buttonPlus.hidden = YES;


NSDictionary *dict = countArr[indexPath.row];
cell.txtFldOne.text = dict[@"TextFieldOne"];
cell.txtFldTwo.text = dict[@"TextFieldTwo"];

cell.txtFldTwo.delegate = self;
cell.txtFldOne.delegate = self;
cell.txtFldOne.tag = indexPath.row;
cell.txtFldTwo.tag = indexPath.row+1000;
[cell.buttonPlus addTarget:self action:@selector(plusBtnClick) forControlEvents:UIControlEventTouchUpInside];
[cell.buttonMinus addTarget:self action:@selector(minusBtnClick:) forControlEvents:UIControlEventTouchUpInside];

cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

  return 150;


-(void)plusBtnClick

dataArr = [NSMutableDictionary dictionary];
[dataArr setValue:@"" forKey:@"TextFieldOne"];
[dataArr setValue:@"" forKey:@"TextFieldTwo"];
[countArr addObject:dataArr];
[viewsTbl reloadData];


-(IBAction)minusBtnClick:(id)sender

CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:viewsTbl];
NSIndexPath *indexPath = [viewsTbl indexPathForRowAtPoint:buttonPosition];
NSLog(@"%@",indexPath);
[countArr removeObjectAtIndex:indexPath.row];
[viewsTbl reloadData];
if (countArr.count == 0)

    addBtn.hidden = NO;
    viewsTbl.hidden = YES;


else

    addBtn.hidden = YES;
    viewsTbl.hidden = NO;




-(IBAction)addBtnClick:(id)sender

dataArr = [NSMutableDictionary dictionary];
[dataArr setValue:@"" forKey:@"TextFieldOne"];
[dataArr setValue:@"" forKey:@"TextFieldTwo"];
[countArr addObject:dataArr];
addBtn.hidden = YES;
viewsTbl.hidden = NO;
[viewsTbl reloadData];

现在的问题是,我在视图控制器中添加了一个名为 print 的按钮并单击它,在文本字段中输入的数据必须添加到字典或数组中。

【问题讨论】:

【参考方案1】:

您还可以使用文本文件委托保存数据

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string

CGPoint buttonPosition = [textField convertPoint:CGPointZero toView:viewsTbl];
NSIndexPath *indexPath = [viewsTbl indexPathForRowAtPoint:buttonPosition];
NSString *text = [textField.text stringByReplacingCharactersInRange:range withString:string];
dataArr = [NSMutableDictionary dictionaryWithDictionary:countArr[indexPath.row]];
 NSMutableDictionary *datadict = [NSMutableDictionary dictionary];
if (number == indexPath.row)

    [datadict setValue:text forKey:@"TextFieldOne"];
    [datadict setValue:dataArr[@"TextFieldTwo"] forKey:@"TextFieldTwo"];


else if(number == indexPath.row +1000)

    [datadict setValue:dataArr[@"TextFieldOne"] forKey:@"TextFieldOne"];
    [datadict setValue:text forKey:@"TextFieldTwo"];


[countArr replaceObjectAtIndex:indexPath.row withObject:datadict];

return YES;

- (void)textFieldDidBeginEditing:(UITextField *)textField

 number = textField.tag;

【讨论】:

以上是关于如何添加多个视图和删除视图并在删除后从视图中获取所有数据?的主要内容,如果未能解决你的问题,请参考以下文章

如何删除最后一行并在表格视图中添加更多行?

添加、删除子视图并在子视图和主 uiviewcontroller 之间进行通信

如何使用显示多个对象项的视图并在从 Django 数据库中删除其中一项后显示 html?

如何删除视图和更新约束?

如何让地图视图中的应用程序用户删除多个图钉?

从多个视图向 UINavigationBar 添加和删除 UIBarButtonItems