ios ios5 uitableview custom-cell

ios - Acceso a atributos de celda fuera de cellForRowAtIndexPath



ios5 uitableview (5)

Tengo cinco campos configurados en un controlador de inscripción. Nombre de usuario, nombre para mostrar, contraseña, confirmar contraseña y dirección de correo electrónico.

Están configurados con lo siguiente:

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;

Las células en sí funcionan bien. Luego tengo alguna validación en un botón que comprueba una dirección de correo electrónico válida, etc., esto también funciona bien.

Lo que me gustaría hacer es actualizar el atributo cell.textLabel.textColor en el código de validación. Por ejemplo, si la dirección de correo electrónico no es válida, deseo actualizar el color del texto de la etiqueta a Rojo para que se destaque (también ordeno el respondedor, etc.).

¿Cómo obtengo una referencia a esta celda específica fuera de la configuración de las celdas?

EDITAR Este es el cambio después de una respuesta. Necesito acceder al quinto cll (ruta de índice 4) en la sección 1 (solo una sección en la vista de tabla):

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


Acceda a la etiqueta personalizada en UiTableView "didselectRow"

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

Obtener ruta de índice desde el botón

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


Simplemente llame a [self tableView:self.tableView cellForRowAtIndexPath:indexPath] . Devolverá la celda en la ruta de índice dada.


puede obtener UITableViewCell usando 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];

Espero que te ayude


Puede crear usando etiquetas.

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

después de eso puedes crear un método de acción de botón como este,

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


Swift actualizado,

let cell = tableView.cellForRow(at: indexPath) as! <Insert tableviewcell Controller here>

Luego puede acceder a todos los objetos dentro de su celda de la misma manera que cuando está en cellforrowat de cellforrowat

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