2013-04-25 12 views
5

Mam pięć pól konfiguracji na kontrolerze rejestracji. Nazwa użytkownika, nazwa wyświetlana, hasło, potwierdź hasło i adres e-mail.Uzyskiwanie dostępu do atrybutów komórki poza komórkąForRowAtIndexPath

Są setup z następujących czynności:

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; 

Komórki sami działać prawidłowo. Następnie mam kilka sprawdzania poprawności na przycisku, który sprawdza poprawny adres e-mail itp., To również działa dobrze.

Co chciałbym móc zrobić, to zaktualizować atrybut cell.textLabel.textColor w kodzie sprawdzania poprawności. Na przykład, jeśli adres e-mail nie jest prawidłowy, chcę zaktualizować kolor tekstu etykiety na czerwony, tak aby się wyróżniał (sortuję też odpowiedź itp.).

Jak uzyskać odniesienie do tej konkretnej komórki poza konfiguracją komórek?

EDYCJA Jest to zmiana wynikająca z odpowiedzi. Muszę uzyskać dostęp do 5th CLL (ścieżka indeks 4) w pkt 1 (tylko jednej sekcji w widoku tabeli):

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

Zmień NSIndexPath * indexPath = [NSIndexPath indexPathForRow: 4 inSection: 1]; jako NSIndexPath * indexPath = [NSIndexPath indexPathForRow: 4 inSection: 0]; –

Odpowiedz

8

można uzyskać UITableViewCell za pomocą 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]; 

Mam nadzieję, że to pomoże.

+0

Otrzymuję błąd SIGBRT w ostatnim wierszu: "NSInternalInconsistencyException", powód: "próba usunięcia wiersza 4 z sekcji 1, ale są tylko 1 sekcje przed aktualizacją" – StuartM

+0

Mam zaktualizowane pytanie z kodem używanym na twoje odpowiedź, ale wygeneruj ten błąd, który nie ma sensu. – StuartM

+0

Ile sekcji znajduje się w twoim stole? a jeśli jest tylko jedna sekcja, to przepuść sekcję jako 0, a nie 1. –

0

Wystarczy zadzwonić [self tableView:self.tableView cellForRowAtIndexPath:indexPath]. Zwróci komórkę na podanej ścieżce indeksu.

0

Etykieta Niestandardowe dostęp w UITableView "didselectRow"

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

uzyskać ścieżkę indeksu z przycisku

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

można tworzyć za pomocą tagów.

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

po tym można utworzyć metody działania przycisku takiego,

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

Updated Swift,

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

Następnie można uzyskać dostęp do wszystkich obiektów wewnątrz komórka w taki sam sposób, jak w przypadku, gdy jesteś w cellforrowat

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