2016-08-31 2 views
2

사용자 정의 셀이있는 테이블 뷰가 있습니다. 각 셀에는 두 개의 텍스트 필드가 있습니다. 표 아래에 버튼이 있습니다. 나는 테이블 뷰 컨트롤러에 "buttonPressed"액션을 가지고있다. 이 메서드에서 각 테이블 행의 텍스트 필드 값을 가져올 수 있습니까?Objective-C : 각 테이블 행의 텍스트 필드 값 가져 오기

- (IBAction)saveBtnPressed:(id)sender{ 
    // Store the text field values of each row in an array here 
} 

누군가가 나를 도울 수 :

... 

@interface CustomCell : UITableViewCell 
@property (weak, nonatomic) IBOutlet UITextField *name; 
@property (weak, nonatomic) IBOutlet UITextField *age; 

@end 

그리고 테이블 뷰 컨트롤러의

나는이 방법을 가지고 : 여기

사용자 정의 셀 헤더 파일입니까?

+0

당신이 테이블 뷰 셀에서 버튼을 클릭하면 TextField를 보여 하시겠습니까? – user3182143

+0

표시되지 않으며 사용자가 각 입력란에 텍스트를 추가해야합니다. 그리고 버튼을 클릭하면 텍스트가 배열에 저장됩니다. – Nono

답변

5

ios 디자인 패턴에 따르면 사용자 지정 셀의 대리자에 배열의 텍스트 필드 값을 저장해야합니다. 그러나 예를 들어 모든 텍스트 필드 값을 같은 위치로 원하면 아래가 해결책입니다. saveBtnPressed 동작이 customController가 아닌 viewController에 있다고 가정합니다. 5 개의 행이 있다고 가정합니다.

- (IBAction)saveBtnPressed:(id)sender{ 

     NSMutableArray *userDetails = [[NSMutableArray alloc] init]; 
     for(i=0;i<5;i++){ 
      CustomCell *cell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]]; 
      NSDictionary *userDictionary = [[NSDictionary alloc] initWithObjectsAndKeys: cell.name.text, @"name", cell.age.text, @"age", nil]; 
      [userDetails addObject:userDictionary]; 
     } 
} 

userDetailsArray에는 모든 셀에 대한 세부 정보가 있습니다. 단계 아래

+0

보이는 행/셀에만 적용 –

+0

예, 동일한 셀이 비 큐이며 다음 값을 표시하기 위해 다시 사용되므로 보이지 않는 UIControl과 그 값을 해제합니다. 보이는 세포의 가치 만 얻을 것입니다. – sschunara

0

시도 : 모두에 textField에 대한 cellForRowAtIndexPath에서

설정 태그입니다. 행의 수와 동일한 것

루프는 dataSource 통해 배열 회수. 루프에서

가져 오기 전지는 태그를 사용하여 텍스트 필드를 얻을 사전의 배열에 텍스트를 저장 :

- (IBAction)OnsaveCilck:(id)sender{ 

    NSMutableArray *Array = [NSMutableArray array]; 

    for (int i=0; i < YourDataSourceArray.Count; i++){ 

     NSIndexPath *indexPath = [NSIndexPath indexPathForRow: i inSection: 0]; 

     CustomCell *cell = [tableview cellForRowAtIndexPath:indexPath]; 

     NSMutableDictionary *Dict = [NSMutableDictionary dictionary]; 

     UITextField *TextName= (UITextField*)[cell.contentView viewWithTag:100];//tag given cellForRowAtIndexPath 
     UITextField *TextAge= (UITextField*)[cell.contentView viewWithTag:200];//tag given cellForRowAtIndexPath 

     [Dict setObject:TextName.text forKey:@"name"]; 
     [Dict setObject:TextAge.text forKey:@"age"]; 

     [Array addObject:TempDict]; 

    } 

    //Final array contains all data 

    NSLog(@"array == %@",Array); 
} 
관련 문제