2013-04-25 11 views
5

Ho cinque campi di configurazione su un controller di registrazione. Username, displayname, password, conferma password e indirizzo email.Accesso agli attributi di cella all'esterno di cellForRowAtIndexPath

Sono configurazione con il seguente:

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; 

le cellule stesse funzionano bene. Ho quindi qualche convalida su un pulsante che controlla la presenza di un indirizzo email valido, ecc. Anche questo funziona bene.

Quello che vorrei essere in grado di fare è aggiornare l'attributo cell.textLabel.textColor nel codice di convalida. Ad esempio, se l'indirizzo e-mail non è valido, voglio aggiornare il colore del testo dell'etichetta a Rosso in modo che si distingua (ho anche ordinare il risponditore, ecc.).

Come si ottiene un riferimento a questa cella specifica al di fuori dell'impostazione delle celle?

MODIFICA Questa è la modifica che segue una risposta. Ho bisogno di accedere al 5 ° CLL (percorso dell'indice 4) nella sezione 1 (una sola sezione in vista tabella):

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

Cambia NSIndexPath * indexPath = [NSIndexPath indexPathForRow: 4 inSection: 1]; as NSIndexPath * indexPath = [NSIndexPath indexPathForRow: 4 inSection: 0]; –

risposta

8

è possibile ottenere UITableViewCell utilizzando 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]; 

Spero che ti aiuti.

+0

Viene visualizzato un errore SIGBRT nell'ultima riga: "NSInternalInconsistencyException", motivo: "tenta di eliminare la riga 4 dalla sezione 1, ma ci sono solo 1 sezioni prima dell'aggiornamento" – StuartM

+0

Ho aggiornato la domanda con il codice che ho utilizzato per rispondi ma genera questo errore che non ha senso. – StuartM

+0

Quante sezioni ci sono nel tuo tavolo? e se c'è solo una sezione allora passa la sezione come 0 non 1. –

0

basta chiamare [self tableView:self.tableView cellForRowAtIndexPath:indexPath]. Restituirà la cella nel percorso dell'indice specificato.

0

accesso etichetta personalizzata In UITableView "didselectRow"

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

ottenere il percorso indice dalla tasto

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

} 
0

È possibile creare utilizzando i tag.

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

dopo che è possibile creare metodo di azione pulsante mi piace questo,

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

Aggiornato Swift,

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

Quindi è possibile accedere a tutti gli oggetti all'interno del vostro cellulare allo stesso modo di quando sei in cellforrowat

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