访问cellForRowAtIndexPath之外的单元属性

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

它们设置如下:

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; 

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

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

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

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

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

希望它能帮到你。

只需调用[self tableView:self.tableView cellForRowAtIndexPath:indexPath] 。 它将返回给定索引路径的单元格。

在UiTableView中访问自定义标签“didselectRow”

 UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; UILabel *label = (UILabel*)[cell viewWithTag:101]; // you can get label like label.backgroundColor=[UIColor redColor]; 

从按钮获取索引路径

 - (void) cellEditAction:(UIButton *)sender { CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:FriendsTable]; NSIndexPath *indexPath = [FriendsTable indexPathForRowAtPoint:buttonPosition]; UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; UILabel *label = (UILabel*)[cell viewWithTag:101]; // you can get label like label.backgroundColor=[UIColor redColor]; } 

您可以使用标签创建。

在cellForRowAtIndexPath中

 static NSString *cellId = @"Cell"; CustomCell *cell = (CustomCell *)[self.tblView dequeueReusableCellWithIdentifier:cellId]; if (cell == nil) { NSArray * myNib; myNib =[[NSBundle mainBundle]loadNibNamed:@"CustomCell" owner:self options:nil]; cell = (CustomCell *)[myNib lastObject]; } [cell.deleteBtn addTarget:self action:@selector(cellDeleteAction:) forControlEvents:UIControlEventTouchUpInside]; cell.deleteBtn.tag = indexPath.row; [cell.editBtn addTarget:self action:@selector(cellEditAction:) forControlEvents:UIControlEventTouchUpInside]; cell.editBtn.tag = indexPath.row; 

之后你可以像这样创建按钮动作方法,

 - (void) cellEditAction:(UIButton *)sender { UIButton *button = (UIButton *)sender; NSString *rateId = [[resDict valueForKey:@"value"]objectAtIndex:button.tag]; } 

更新了Swift,

 let cell = tableView.cellForRow(at: indexPath) as!  

然后,您可以像在cellforrowat一样访问单元格内的所有对象

 let displayname = cell.labelfordisplayname.text! print("This is the displayname -> \(displayname)")