2013-12-11 53 views
0

Here is the View細胞編程方式操縱的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(「名」)消失,且的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修正:) – envinyater

回答

0

在我看來,你正在定義靜態單元格的tableview。在這種情況下,您不需要爲tableview實現數據源委託方法,並且如果您確實覆蓋了您在故事板中定義的靜態單元格。如果您需要爲這些單元格設置內容,我建議您爲單元格定義一個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 
相關問題