2013-12-11 3 views
0

Here is the ViewProgramatically манипулируя UITextField в клетке, которая была создана в раскадровке

Я пытаюсь изменить UITextField в качестве делегата TableView

- (UITableViewCell *)tableView:(UITableView *)tableView 
     cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString * CellIdentifier = @"CellIdentifier"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
             reuseIdentifier:CellIdentifier]; 
    } 

    // Configure cell here 
    if (indexPath.section == [self nameSectionNumber]) { 
     if (indexPath.row == [self firstNameRowNumber]){ 
      UITextField *textFieldFromCell = [self retrieveUITextFieldFromCell:cell]; 
      textFieldFromCell.text = [self.person firstName]; 
     } else if (indexPath.row == [self lastNameRowNumber]) { 
      cell.textLabel.text = [self.person lastName]; 
     } 
    } else if (indexPath.section == [self emailSectionNumber]) { 
     if (indexPath.row == [self emailSectionHomeRowNumber]) { 
      cell.textLabel.text = [self.person homeEmail]; 
     } else if (indexPath.row == [self emailSectionWorkRowNumber]) { 
      cell.textLabel.text = [self.person workEmail]; 
     } 
    } 

    return cell; 
} 


- (UITextField *)retrieveUITextFieldFromCell:(UITableViewCell *)cell 
{ 
    // Need to grab UITextField from cell 
    for (UIView *view in cell.contentView.subviews){ 
     if ([view isKindOfClass:[UITextField class]]){ 
      UITextField* txtField = (UITextField *)view; 
      return txtField; 
     } 
    } 
    return nil; 
} 

Если я не пытаюсь изменить ячейку и просто сделать

// Configure cell here 
    if (indexPath.section == [self nameSectionNumber]) { 
     if (indexPath.row == [self firstNameRowNumber]){ 
      cell.textLabel.text = [self.person firstName]; 
     } else if (indexPath.row == [self lastNameRowNumber]) { 
      cell.textLabel.text = [self.person lastName]; 
     } 
    } 

UILabel («First Name») ушел и UITextField нет, чего я ожидал, так как я думаю, что его из-за этого кода

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
     if (cell == nil) { 
      cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
              reuseIdentifier:CellIdentifier]; 
     } 

Second view

Примечание: Игнорировать фамилию и адрес электронной поля. На данный момент я только фокусируюсь на имени, чтобы это выглядело правильно.

+0

'cell.textLabel.text.text'? – nhgrif

+0

Не совсем ясно, что вы пытаетесь сделать, или ваш вопрос. – rdelmar

+0

@nhgrif Fixed :) – envinyater

ответ

0

Мне кажется, что вы определяете табличное представление со статическими ячейками. В этом случае вам не нужно внедрять методы делегирования источника данных для представления таблицы, и если вы делаете это, вы в основном перезаписываете статические ячейки, которые вы определили в раскадровке. Если вам нужно установить контент для этих ячеек, я предлагаю вам определить IBOutlet для ячейки и просто ссылаться на эту розетку и напрямую изменять свойство текста.

@interface ProfileViewController() 
    @property (weak, nonatomic) IBOutlet UITextField *firstNameTextField; 
    @property (weak, nonatomic) IBOutlet UITextField *lastNameTextField; 
@end 

@implementation ProfileViewController 
    - (void)viewDidLoad 
    { 
     [super viewDidLoad]; 

     self.navigationItem.rightBarButtonItem = self.editButtonItem; 
     _firstNameTextField.text = @"John"; 
     _lastNameTextField.text = @"Smith"; 
    } 

    ... 
@end 
Смежные вопросы