2017-01-12 9 views
0

1つのテキストフィールドで複数のビューを作成したい場合、+ボタンをクリックすると複数のビューを作成する必要があります。また、ボタンをクリックするとビューを削除する必要があります。私はこのコードを使っていました。マルチビューを追加してビューを削除し、削除後にビューからすべてのデータを取得するにはどうすればよいですか?

@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; 
} 
デリゲートを提出
関連する問題