2012-02-13 5 views
0

textField를 사용하여 사용자 정의 셀을 만들었지 만 값을 검색 할 수 없습니다.
내 tableView에는 이러한 기본 셀과 함께 3 개의 맞춤 셀이 있습니다. 이건 내 코드입니다
셀의 textField에서 null 값을 반환했습니다.

NSString *string = customCell.textField.text; 

난 항상 null 값을 얻을 ... :

TextFieldCell.m

#import "TextFieldCell.h" 

@implementation TextFieldCell 

@synthesize label; 
@synthesize textField; 

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 
{ 
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 
    if (self) 
    { 
     label = [[UILabel alloc] initWithFrame:CGRectZero]; 
     label.backgroundColor = [UIColor clearColor]; 
     label.textAlignment = UITextAlignmentRight; 
     label.textColor = [UIColor blackColor]; 
     label.font = [UIFont boldSystemFontOfSize:16.0]; 
     label.adjustsFontSizeToFitWidth = YES; 
     label.baselineAdjustment = UIBaselineAdjustmentNone; 
     label.numberOfLines = 20; 
     [self.contentView addSubview:label]; 

     textField = [[UITextField alloc] initWithFrame:CGRectZero]; 
     textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter; 
     textField.backgroundColor = [UIColor clearColor]; 
     textField.font = [UIFont boldSystemFontOfSize:16.0]; 
     textField.delegate = self; 
     textField.returnKeyType = UIReturnKeyDone; 
     [self.contentView addSubview:textField]; 
    } 

    return self; 
} 

-(void)layoutSubviews 
{ 
    [super layoutSubviews]; 

    CGRect r = CGRectInset(self.contentView.bounds, 8, 8); 
    CGRect r1 = CGRectInset(self.contentView.bounds, 8, 8); 

    if (label.text != nil) 
    { 
     r.size = CGSizeMake(12, 27); 
     label.frame = r; 

     r1.origin.x += self.label.frame.size.width + 6; 
     r1.size.width -= self.label.frame.size.width + 6; 
     textField.frame = r1; 
    } 
    else 
    { 
     textField.frame = r; 
    } 
} 

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

- (void)setSelected:(BOOL)selected animated:(BOOL)animated 
{ 
    [super setSelected:selected animated:animated]; 
} 

@end 

테이블보기 데이터 소스


나는 이것을 시도 할 때
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (indexPath.section == 0) 
    { 
     if (indexPath.row == 0) 
     { 
      return [self textFieldCellForTableView:tableView atIndexPath:indexPath]; 
     } 
     else if (indexPath.row == 1) 
     { 
      return [self textFieldCellForTableView:tableView atIndexPath:indexPath]; 
     } 
     else 
     { 
      return [self defaultCellForTableView:tableView atIndexPath:indexPath]; 
     } 
    } 
    else if (indexPath.section == 1) 
    { 
     if (indexPath.row == 0) 
     { 
      return [self textFieldCellForTableView:tableView atIndexPath:indexPath]; 
     } 
     else 
     { 
      return [self defaultCellForTableView:tableView atIndexPath:indexPath]; 
     } 
    } 
    else if (indexPath.section == 2) 
    { 
     return [self defaultCellForTableView:tableView atIndexPath:indexPath]; 
    } 
    else 
    { 
     return [self chooseFotoCellForTableView:tableView atIndexPath:indexPath]; 
    } 
} 

-(TextFieldCell *)textFieldCellForTableView:(UITableView *)tableView atIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *TextFieldCellIdentifier = @"TextFieldCellIdentifier"; 

    TextFieldCell *cell = [tableView dequeueReusableCellWithIdentifier:TextFieldCellIdentifier]; 
    if (cell == nil) 
    { 
     cell = [[[TextFieldCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:TextFieldCellIdentifier] autorelease]; 
     cell.selectionStyle = UITableViewCellSelectionStyleNone; 
    } 

    if (indexPath.section == 0) 
    { 
     if (indexPath.row == 0) 
     { 
      cell.label.text = nil; 
      cell.textField.placeholder = NSLocalizedString(@"Nome", @""); 
     } 
     else 
     { 
      cell.label.text = [[NSLocale currentLocale] objectForKey:NSLocaleCurrencySymbol]; 
      cell.textField.placeholder = NSLocalizedString(@"Costo", @""); 
     } 
    } 
    else 
    { 
     cell.label.text = nil; 
     cell.textField.placeholder = NSLocalizedString(@"URL", @""); 
    } 

    return cell; 
} 

코드 퇴각 rieve 텍스트 필드 값

TextFieldCell *nomeCell = (TextFieldCell *)[self tableView:self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]]; 
TextFieldCell *costoCell = (TextFieldCell *)[self tableView:self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]]; 
TextFieldCell *linkCell = (TextFieldCell *)[self tableView:self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:1]]; 

NSLog(@"nomeCell textField = %@", nomeCell.textField.text); 
NSLog(@"costoCell textField = %@", costoCell.textField.text); 
NSLog(@"linkCell textField = %@", linkCell.textField.text); 

나는 nomeCell, costoCell 및 linkCell를 기록하려고 노력하고 올바르게 할당 및 초기화와 나는 ...
사람이 좀 도와 줄래 여기 ARC를 사용하지 않는거야?
정말 고마워요!

답변

1

, 당신은 값을 검색됩니다

+0

너무 감사합니다! 당신은 내 문제를 해결했다;) – matteodv

0

placeholder 속성에 텍스트를 지정하고 textField.text을 로깅합니다. 난 당신이 실제로 당신이 값을 검색하고자하는 시간을 기준으로 텍스트 필드에 텍스트를 입력했다고 가정

//fetch cell for which you want to get textfield then, 
TextFieldCell *nomeCell = (TextFieldCell *)[cell.contentView viewWithTag:101]; 
0
textField = [[UITextField alloc] initWithFrame:CGRectZero]; 
     textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter; 
     textField.backgroundColor = [UIColor clearColor]; 
     textField.font = [UIFont boldSystemFontOfSize:16.0]; 
     textField.delegate = self; 

     //added 
     textField.tag = 101; 

     textField.returnKeyType = UIReturnKeyDone; 
     [self.contentView addSubview:textField]; 

코드는 텍스트 필드 값을 검색 데이터를 저장하기 위해 표보기/셀에 의존하기보다는 데이터 모델을 업데이트 하시겠습니까?

테이블 뷰 컨트롤러의 tableView:cellForRowAtIndexPath:을 호출하여 현재 셀을 가져 오지 마십시오. 새 셀이 필요할 때만 테이블보기에서 호출해야합니다.

테이블보기에서 cellForRowAtIndexPath: 메서드를 직접 호출하십시오. 대신이의 :

TextFieldCell *nomeCell = (TextFieldCell *)[self tableView:self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]]; 

는이 작업을 수행 :

TextFieldCell *nomeCell = (TextFieldCell *)[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]]; 
관련 문제