2012-12-24 2 views
1

나는 textField를 포함하도록 사용자 정의 된 셀을 동적으로 생성하는 테이블을 만들었습니다. 프로그램을 실행하면 textField에 텍스트를 입력 할 수 있습니다. 그러나 프로그램을 종료하거나 다른 viewController로 전환하기 전에 입력 된 텍스트를 수집 할 수 없습니다. 당신은 사용자가 입력 한 텍스트를 추출하기 위해 내가해야 할 것을 제안 해 주실 수 있습니까?셀에 포함 된 textField에 액세스하기

나는 다음과 같은 코드를 사용하여 세포에 액세스 할 수 있는지 이해 ...

for (int section = 1; section < [self.tableView numberOfSections]; section++) // section 0: profile picture 
{ 
    for(int row = 0; row < [self.tableView numberOfRowsInSection:section]; row++) 
    { 
     NSLog(@"section = %d, row = %d", section, row); 
     NSIndexPath *tempIndexPath = [NSIndexPath indexPathForRow:row inSection:section]; 
     UITableViewCell *tempCell = [self tableView:self.tableView cellForRowAtIndexPath:tempIndexPath]; 
//   NSLog(@"tempCell = %@", tempCell); 

    } 
} 

그러나 나는 그들에 포함 된 텍스트를 추출 할 수 없습니다입니다.

나는 또한 : Accessing UITextField in a custom UITableViewCell을 언급했다. 그러나 나는 더 깨끗한 해결책을 찾고있다.

감사합니다.

+0

당신이 사용자 정의 셀 클래스의 속성을 @ 사용하여 게터 세터와 텍스트 필드를 만들려고했고, 이러한 사용 tempcell 액세스 : 여기

, 그냥 스토리 보드에 태그를 설정하는 것을 잊지 당신이 할 수있는 일입니다 .yourtextfield.text. – josh

답변

1

당신이 참조하는 링크는 당신이해야 할 일에 매우 가깝지만, indexPath를 얻는 더 좋은 방법이 있습니다.

iOS 프로그래밍을 시작할 때 데이터가 필요할 때 (예 : 사용자가 '제출'을 누르는 경우) 텍스트 입력란의 값을 모두 가져와야한다는 잘못된 생각이 있습니다. 특히 테이블에있을 때 문제는 텍스트 필드를 항상 사용할 수 없다는 것입니다. 셀이 화면에서 벗어난 경우 셀이 존재하지 않거나 테이블의 다른 행에서 재사용됩니다. 텍스트 필드는 보기입니다. 표시 데이터이며, 사용자가 모델을 저장하는 곳으로 사용하지 마십시오. (

귀하의 .H 파일 :

그래서, 당신이 가장 먼저해야 할 일은 UITextFieldDelegate 프로토콜보기 컨트롤러 준수하고보기 컨트롤러에 당신이 그것을 만들 때 텍스트 필드의 대리자를 설정하는 것입니다 당신이 당신의 텍스트 필드를 만들 때

@interface YourViewController : UIViewController <UITextFieldDelegate> 

가 :

myNewTextfield.delegate = self; 

이 텍스트를 알려주는 <UITextFieldDelegate>는 중요한 부분)입니다 필드를 사용하여 중요한 변경 사항을 알려줍니다.

- (void) textFieldDidEndEditing:(UITextField *)textField { 
    // If you need the index path of the table view cell which contains the text field in order to know how to store it, use: 
    CGRect position = [self convertRect:textField.frame toView:self.tableView]; 
    NSArray *indexPaths = [self.tableView indexPathsForRowsInRect:position]; 
    NSIndexPath *indexPath = [indexPaths objectAtIndex:0]; 

    // Save the contents of the text field somewhere so that you have it later when you need it: 
    something = textField.text; 
} 
0

This 튜토리얼 : 지금, 당신은 당신이 텍스트를 저장할 수 있도록 그들이 텍스트 필드 편집을 완료하는 즉시 라고 텍스트 필드의 대리자 메서드를 만들고 호출 할 때까지 기다릴 필요 나에게 도움이되었다. 태그를 통해 필요한 객체를 참조 할 수 있습니다.

UIImageView 또는 UITextField 등을 드래그하여 태그를 100 (원하는대로)로 설정 한 다음 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath에서 태그를 사용하여 참조하십시오.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
static NSString *CellIdentifier = @"Cell"; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

// Configure the cell... 
if (cell == nil) { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
} 

UITextField *tField = (UITextField *)[cell viewWithTag:100]; 

return cell; 
} 
관련 문제