【问题标题】:returning null value from cell's textField从单元格的 textField 返回空值
【发布时间】:2012-02-13 01:45:22
【问题描述】:

我创建了一个带有 textField 的自定义单元格,但我无法检索它的值。
在我的 tableView 中,有 3 个这样的自定义单元格,以及其他默认单元格。
当我尝试这个时:

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

检索文本字段值的代码

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...
谁能帮帮我?
非常感谢!

【问题讨论】:

    标签: iphone uitableview uitextfield custom-cell


    【解决方案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]];
    

    【讨论】:

      【解决方案2】:

      您正在将文本分配给placeholder 属性并且您正在记录textField.text

      【讨论】:

        【解决方案3】:
        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];
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2020-07-12
          • 2011-01-27
          • 2012-12-21
          • 1970-01-01
          • 2016-01-06
          • 1970-01-01
          • 2016-12-27
          相关资源
          最近更新 更多