2012-08-01 7 views
1

스토리 보드를 사용하여 정적 인 UITableView를 생성했습니다. 표보기의 각 행에는 텍스트 필드가 포함되어 있으며 각 행에 식별자를 부여했습니다. 텍스트 필드 중 하나는 날짜를 입력하는 데 사용됩니다. 이 텍스트 필드가 선택되면 UIDatePicker를 표시하고 싶습니다. 이 작업을 수행하기 위해 텍스트 필드의 inputview를 UIDatePicker로 설정하려고합니다. 나는이 특정 UITableViewCell에 대한 UITextField를 얻는 것을 고심하고있다. 여기에 내가가는 방향이 있습니다 :UITableViewCell에서 UITextField에 대한 참조를 얻으려면 어떻게해야합니까?

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    const NSString *birthDateCellIdentifier = @"BirthDateCell"; 
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
    NSString * cellIdentifier = [cell reuseIdentifier]; 
    if(cellIdentifier isEqualToString:birthDateCellIdentifier){ 
     /*this is the cell we're after so get pointer to cell's textfield*/ 
     //set textfield's inputview to UIDatePicker 

    } 


} 

이것이 올바른 접근 방법입니까? 그렇다면 어떻게이 셀에 텍스트 필드를 찾을 수 있습니까?

감사합니다.

답변

1

아니요, 아닙니다. 각 셀에 대해 다른 재사용 식별자를 사용하면 테이블 wiev가 셀을 대기열에 추가 할 수 없기 때문에 메모리를 낭비하게됩니다. 대신 tag 속성을 사용하십시오.

셀을 찾았 으면 텍스트 필드를 속성으로 추가해야합니다. 가장 깨끗한 방법은 셀에 액세스하는 것입니다.

+0

감사합니다. IB (스토리 보드) 내에 셀을 만들었습니다. 그렇다면 텍스트 필드가 이미 셀의 속성이되어서는 안됩니까? – Nick

+1

코드에서 선언하지 않았 으면 (또는 아직없는 경우), 그렇지 않습니다. (그래서 내가 일반적으로 초보자를 위해 IB 사용을 꺼리는 이유입니다. ** 혼란 스럽습니다 **.) –

+0

혼란 스럽습니다. 나는 그것을 – Nick

0

당신이 jQuery과 가난한 선택이있을 가능성을 고려 되세요 이 특별한 문제가 있습니까?

생각해보십시오. 각 "셀"은 다르게 보이고 다르게 작동하며 다른 목적을 수행하는 것으로 보이며 동적 데이터 바인딩이 필요없는 고정 된 숫자가 있습니다.

내가 너라면, 각 입력 필드에 대한 사용자 지정보기를 만들고 UIScrollView에서 전체를 훑어보기 만하면됩니다.

이 방법을 확고히하더라도 문제가 있습니다. UITableViewCells UITextField 속성이 없으므로 액세스 할 수 없습니다.

@interface DatePickerView : UIView <UITextFieldDelegate> 

@property (nonatomic, retain) UITextField *textField; 
@property (nonatomic, retain) UIDatePicker *datePicker; 

@end 

이제는 모든 로직이 DatePickerView 클래스에 포함됩니다. UITextFieldDelegate 메서드를 구현하여 선택한 텍스트 필드에 대해 걱정할 필요없이 입력을 처리 할 수 ​​있습니다. DatePickerView의 구현, 당신은 할 수 있습니다 :

- (void)textFieldDidBeginEditing:(UITextField *)textField 
{ 
    // Show the UIDatePicker. 
} 

이 당신에게 더 깨끗하고 더 나은 포함 된 디자인을 제공 할 것입니다.

당신은 UIDatePickerUITextFieldinputView 속성을 설정하기위한 indexPath을 사용할 수 있습니다
1

,

이 코드는 당신을 도울 수 ...

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *identifier = @"Identifier"; 
    UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:identifier]; 
    if (cell == nil) { 

     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier]; 
    } 

    UITextField *textField = [[[UITextField alloc] initWithFrame:CGRectMake(10, 10, 200, 20)] autorelease]; 
    textField.textColor = [UIColor darkTextColor]; 
    textField.tag = indexPath.row; 
    textField.delegate = self; 
    [cell.contentView addSubview:textField]; 

    if (indexPath.row ==0) { 
     // Setting Datepicker as a inputView to the textfield on first ell 
     UIDatePicker *datePicker = [[[UIDatePicker alloc] init] autorelease]; 
     [datePicker addTarget:self action:@selector(dateChanged:) forControlEvents:UIControlEventValueChanged]; 
     textField.inputView = datePicker; 
    }else if(indexPath.row == 1) { // Set the Number pad for the textfield on second cell 
     textField.keyboardType = UIKeyboardTypeNumberPad; 
    } 

    return cell; 
} 


// DatePicker selector 
-(void)dateChanged:(id)sender 
{ 
    UIDatePicker *picker = (UIDatePicker *)sender; 
    NSDate *date =picker.date; 
    NSLog(@"selected date: %@", date); 
} 



// TextField Delegate methods 
-(void)textFieldDidBeginEditing:(UITextField *)textField 
{ 

} 
+0

에게 줄 것이다. 그렇다면 dateChanged 메소드에서 텍스트를 선택한 날짜로 설정하기 위해 textField에 대한 참조를 다시 얻는가? –

관련 문제