2013-10-23 3 views
2

지금 섹션을 설정하고 입력란에 UITextFields의 입력을 가져 오려고 노력하고 있습니다.UITextFields가있는 UITableView 섹션

내 첫 번째보기에서 나는 얼마나 많은 섹션을 갖고 싶은지, 그리고 버튼을 누를 때 UITableView에있는 해당 섹션 (4 행)을 제공하는 UITextField을 가지고 있습니다. 지금 내 문제는 각각 UITextField의 올바른 값을 얻지 못한다는 것입니다. 예를 들어

:

  • 모든 섹션은 INT를 작성하거나 내가 텍스트 필드 1과 2에서 INT를 알고
  • 떠있는 텍스트 필드에
  • 4 텍스트 필드에 있고, 나는 그들을 분할 ... 여기

내 코드는 ....

#import "ViewController.h" 

@interface ViewController() 
{ 
    NSMutableArray *sectionsArray; 
    NSMutableArray *rowsArray; 
    NSMutableArray *textFieldArray; 
} 

@end 

@implementation ViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. 

    [[self myTableView]setDelegate:self]; 
    [[self myTableView]setDataSource:self]; 

    sectionsArray = [[NSMutableArray alloc]init]; 
    rowsArray = [[NSMutableArray alloc]initWithObjects:@"",@"",@"",@"", nil]; 
    textFieldArray = [[NSMutableArray alloc]initWithObjects:@"",@"",@"",@"", nil]; 

} 

- (void)viewWillAppear:(BOOL)animated 
{ 
    [super viewWillAppear:animated]; 
    [self.myTableView reloadData]; 
} 


- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

#pragma mark - Table View 

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 

    int anzahl = [_AnzahlStationen intValue];//from first view...anzahl=quantity of sections 

    //adds sections 
    do { 

     [sectionsArray addObject:[NSString stringWithFormat:@"Palette %lu",sectionsArray.count+1]]; 

    } while ([sectionsArray count] < anzahl); 

    return sectionsArray.count; 
} 


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

    return rowsArray.count; 
} 


-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 
{ 
    return [sectionsArray objectAtIndex:section]; 
} 


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"]; 
    if (cell==nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"]; 
     cell.selectionStyle = UITableViewCellSelectionStyleNone; 

     UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, 300, 24)]; 
     textField.font = [UIFont fontWithName:@"Gujarati Sangam MN" size:15]; 
     textField.textColor = [UIColor redColor]; 

     textField.autoresizingMask = UIViewAutoresizingFlexibleWidth; 
     textField.tag = 17; 
     [cell.contentView addSubview:textField]; 
     textField.delegate = self; 
    } 

    UITextField *textField = (UITextField *)[cell.contentView viewWithTag:17]; 

    textField.text = [textFieldArray objectAtIndex:indexPath.row]; 

    return cell; 
} 

#pragma mark - UITextFieldDelegate 

- (BOOL)textFieldShouldReturn:(UITextField *)textField { 
    [textField resignFirstResponder]; 
    return YES; 
} 

- (void)textFieldDidEndEditing:(UITextField *)textField { 

    [textFieldArray replaceObjectAtIndex:0 withObject:textField.text]; 
} 

@end 
,

올바른 행의 올바른 UITextField를 어떻게 바꿀 수 있습니까? ,

NSMutableArray *tableViewData = [NSMutableArray array]; 

[tableViewData addObject:@[ @{@"textfield" : YourTextField} ]]; 
[tableViewData addObject:@[ @{@"textfield" : YourTextField} ]]; 
[tableViewData addObject:@[ @{@"textfield" : YourTextField} ]]; 
[tableViewData addObject:@[ @{@"textfield" : YourTextField} ]]; 

위의 코드는 4 개 배열을 포함, tableViewData 나타내는 각 배열은 단일 셀의 데이터를 유지 한 사전을 포함

+0

이상적으로 데이터 소스 콜백에서 데이터 소스에 개체를 추가해서는 안됩니다. 왜 viewDidLoad에서 그렇게하지 않습니까? 귀하의 질문은 당신이 무엇을 요구하고 있는지 이해하기가 어렵습니다. – Tim

+0

그래서 먼저 viewdidload에 섹션을 추가해야합니까? – user2912794

+0

다른 섹션 데이터를 다른 곳에 만들면 위임 콜백이 그 방식으로 카운트를 검색 할 수 있습니다. 귀하의 실제 질문의 측면에서, 난 아직도 이해가 안 돼 – Tim

답변

1

난 당신이 유사한 데이터 소스를 사용하는 것이 좋습니다. 필자는 예제에서 하나의 키 - 값 쌍인 UITextField을 사용했습니다.

원하는대로 동적으로 만들 수 있으며 대리인 및 데이터 소스 메서드에서이를 읽을 수 있습니다. 3 개의 개별 배열을 가지는 것보다.

예. 위의 방법을 사용하여

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return [tableViewData count]; 
} 

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return [[tableViewData objectAtIndex:section] count]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSDictionary *cellData = [[tableViewData objectAtIndex:indexPath.section] objectAtIndex:indexPath.row]; 

    // Create your cell and use the data stored in the dictionary 
} 

는이 사전을 업데이트하고, 하나의 장소에서 UITableView의 모든 상태를 유지할 수 있습니다, 그것은 코드가 깔끔하고 읽기 유지합니다. UITextField에 액세스하려면 UITableView 데이터 소스 및 위임 메소드의 사전에서 찾으십시오.

희망이 도움이됩니다.

+0

대단히 감사합니다! 나는 일을 한 후에 봐야 할 것입니다. 나는 이것을 이해할 시간이 필요합니다 ... – user2912794

+0

지금까지는 모든 것이 잘 작동합니다. 섹션 제목과 번호로 내 섹션을 얻지 만, 이제 어떻게하면 값을 얻을 수 있는지 이해하지 못합니다. 텍스트 필드 및보기를 스크롤 할 때 잃어 버리지 않는 텍스트 필드에 저장하는 방법에 대해 설명합니다. – user2912794

+0

NSDictionary에서 TextField 내부의 텍스트를 업데이트하면 스크롤 할 때 잃지 않을 willDisplayCell에서 읽습니다. – Tim