2017-01-12 53 views
0

我想创建一个文本字段的多个视图,现在我必须创建多个视图,当我点击+按钮,如果你点击 - 按钮,视图必须删除。我确实使用了这个代码。如何在删除后添加多视图并删除视图并从视图中获取所有数据?

@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]; 
} 

现在的问题是,我在视图控制器中添加一个名为打印按钮,点击它在文本字段中输入的数据必须添加到任何字典或阵列。

回答

0

您还可以节省数据使用文本提交代表

- (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; 
} 
相关问题