2012-02-13 87 views
0

我用textField創建了一個自定義單元格,但是我無法檢索它的值。
在我的tableView中,有3個這些自定義單元格,以及其他默認單元格。
當我試試這個:從單元格的textField返回空值

NSString *string = customCell.textField.text; 

我總是得到一個空值...
這是我的代碼:

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; 
} 

Code To Ret 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

我假設你已經真正進入了您要檢索值的時間在文本字段中一些文本,您檢索值更新你的數據模型,而不是依靠表格視圖/單元格來存儲你的數據?

不要通過調用您的表視圖控制器的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]]; 
+0

太感謝你了!你解決了我的問題;) – matteodv 2012-02-13 14:33:57

0

您正在將文本指定給placeholder屬性,並且您正在記錄textField.text

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]; 

代碼來檢索文本字段值:

//fetch cell for which you want to get textfield then, 
TextFieldCell *nomeCell = (TextFieldCell *)[cell.contentView viewWithTag:101]; 
相關問題