访问cellForRowAtIndexPath之外的单元属性 [英] Accessing cell attributes outside of cellForRowAtIndexPath

查看:129
本文介绍了访问cellForRowAtIndexPath之外的单元属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Signup控制器上设置了五个字段。用户名,显示名称,密码,确认密码和电子邮件地址。

I have five fields setup on a Signup controller. Username, displayname, password, confirm password and email address.

它们设置如下:

static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.textLabel.textColor = [UIColor colorWithRed:14.0f/255.0f green:62.0f/255.0f blue:178.0f/255.0f alpha:0.8f];
    cell.textLabel.font = [UIFont boldSystemFontOfSize:13.0f];


    switch ( indexPath.row ) {
        case 0: {
            cell.textLabel.text = NSLocalizedString(@"UsernameFieldLabel", @"Username field label");
            [cell addSubview:self.usernameField];
            break ;
        }
        case 1: {
            cell.textLabel.text = NSLocalizedString(@"DisplayNameFieldLabel", @"Displayname field lael");
            [cell addSubview:self.displayNameField];
            break ;
        }
        case 2: {
            cell.textLabel.text = NSLocalizedString(@"PasswordFieldLabel", @"Password field lael");
            [cell addSubview:self.passwordField];
            break ;
        }
        case 3: {
            cell.textLabel.text = NSLocalizedString(@"ConfirmPasswordFieldLabel", @"Confirm Password field lael");
            cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
            cell.textLabel.numberOfLines = 2;
            [cell addSubview:self.confirmPasswordField];
            break ;
        }
        case 4: {
            cell.textLabel.text = NSLocalizedString(@"EmailFieldLabel", @"Email field lael");
            [cell addSubview:self.emailField];
            break ;
        }
    }

    return cell;

细胞本身工作正常。然后我对一个检查有效电子邮件地址等的按钮进行了一些验证,这也可以正常工作。

The cells themselves work fine. I then have some validation on a button which checks for a valid email address etc, this also works fine.

我希望能够做的是更新单元格验证代码中的.textLabel.textColor属性。例如,如果电子邮件地址无效,我想将标签文本颜色更新为红色,以便它突出(我也对响应者等进行排序)。

What I would like to be able to do is update the cell.textLabel.textColor attribute in the validation code. For example, if the email address is not valid I want to update the label text color to Red so that it stands out (I also sort the responder etc).

如何在单元格设置之外我是否会获得对此特定单元格的引用?

How do I get a reference to this specific cell outside of the setup of cells?

编辑
这是答案后的更改。我需要访问第1部分中的第5个cll(索引路径4)(表视图中只有一个部分):

EDIT This is the change following an answer. I need to access the 5th cll (index path 4) in section 1(only one section in table view):

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:4 inSection:1];

UITableViewCell* cell = [self.tableView cellForRowAtIndexPath:indexPath];
cell.textLabel.textColor = [UIColor redColor];
[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];


推荐答案

您可以使用NSIndexPath获取UITableViewCell。

you can get UITableViewCell using NSIndexPath.

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:YOUR_ROW inSection:YOUR_SECTION];

UITableViewCell* cell = [yourTable cellForRowAtIndexPath:indexPath];
cell.textLabel.textColor = YOUR_COLOR;
[yourTable reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];

希望对您有帮助。

这篇关于访问cellForRowAtIndexPath之外的单元属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆