配置自定义单元格方法在自定义单元格中不起作用,我应该使用委托吗?

时间:2013-11-20 21:38:05

标签: ios iphone objective-c uitableview

我已经使用一些自定义单元格UITableView来验证UITextField和日期选择器。我想做一个简单的任务,但我不知道为什么它不起作用。我只想在检查TextField文本属性是否为空时更改UILabel textColor属性。

我使用此代码初始化cellForRowAtIndexPath中的海关单元格,例如:

  firstNameCell = (WPFirstNameCell *)[aTableView dequeueReusableCellWithIdentifier:FirstNameCellIdentifier];

        if(firstNameCell == nil){
            // INIT REUSABLE CELL
            firstNameCell = [[WPFirstNameCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:FirstNameCellIdentifier] ;
            [firstNameCell setAccessoryType:UITableViewCellAccessoryNone];
            firstNameCell.firstDelegate = self;
        }

        if (indexPath.row == 0){
            firstNameCell.firstnameTextField.text = firstN;
     //       firstNameCell.validLabel.text = firstValidation;
            return firstNameCell;
        }

然后我设置使​​用代码创建单元格:

@implementation WPFirstNameCell
@synthesize firstnameTextField;
@synthesize isValid, validLabel;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        isValid = NO;
        validLabel = [[WPCustomLabel alloc] initWithFrame:CGRectMake(20, 10, 80, 30)];
        validLabel.textColor = [UIColor blackColor];
        validLabel.font = [UIFont systemFontOfSize:14.0];
        validLabel.text = @"First Name ";
        [validLabel setTextAlignment:NSTextAlignmentLeft];
        [self addSubview:validLabel];



        firstnameTextField = [[WPCustomTextField alloc] initWithFrame:CGRectMake(110, 10, 240, 30)];
        firstnameTextField.placeholder = @"your first name";
        firstnameTextField.autocorrectionType = UITextAutocorrectionTypeNo;
        [firstnameTextField setClearButtonMode:UITextFieldViewModeWhileEditing];
        firstnameTextField.delegate = self;
        [self addSubview:firstnameTextField];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        //cell.accessoryView = self.firstName;
    }
    return self;
}

在viewController中,我有一个按钮,在发送信息之前,它必须检查textFields是否为空。如果textField为空,我必须更改标签的颜色。

当我按下按钮时,我会调用此方法:

// This method is implemented in the viewController
    -(void)changeColorsOfLabels
    {

    //firstN is the NSString that I get from the TextField

        if(firstN]){
            [firstNameCell changeColorOfLabel:NO];
             [tableView reloadData];
        }
    }

在单元格中,我有这种改变颜色的方法:

-(void)changeColorOfLabel:(BOOL)isOkay
{
    if(isOkay){
        isValid = YES;
        validLabel.textColor = [UIColor blackColor];
    } else {
        isValid = NO;
        NSLog(@"isOK: %hhd", isOkay);
        validLabel.textColor = [UIColor redColor];
    }
}
正确调用了

NSLog。我可以看到良好的输出。

1 个答案:

答案 0 :(得分:0)

一般情况下,你应该总是使用setter:self.validLabel.tewtColor = [UIColor redColor];

此外,您应该在cellForRowAtIndexPath:time执行此配置,该时间由reloadData

调用