gpt4 book ai didi

ios - 访问 cellForRowAtIndexPath 之外的单元格属性

转载 作者:可可西里 更新时间:2023-11-01 03:42:01 24 4
gpt4 key购买 nike

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

它们的设置如下:

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;

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

我希望能够做的是更新验证代码中的 cell.textLabel.textColor 属性。例如,如果电子邮件地址无效,我想将标签文本颜色更新为红色以突出显示(我还对回复者进行排序等)。

如何在单元格设置之外获取对这个特定单元格的引用?

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

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。

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

希望对你有帮助。

关于ios - 访问 cellForRowAtIndexPath 之外的单元格属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16220777/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com