2012-03-24 82 views
1

我想將UItextField放入UITableViewCell中,但是我遇到了問題。文本字段不顯示。這是我的代碼:UITableViewCell中的UITextField

-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString   *)reuseIdentifier { 
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 
    if (self) { 
     self.textField = [[UITextField alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 120.0f, 45.0f)]; 
     self.textField.textColor = [UIColor blackColor]; 

     [self.contentView addSubview:self.textField]; 
    } 

    return self; 
} 


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

    TextFieldCell *cell = (TextFieldCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[TextFieldCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 
    switch (indexPath.row) { 
     case 1: 
      cell.textField.placeholder = @"Username"; 
      break; 
     case 2: 
      cell.textField.placeholder = @"Password"; 
      break; 
     case 3: 
      cell.textField.placeholder = @"Server Address"; 
      break; 
     case 4: 
      cell.textField.placeholder = @"Description"; 
      break;    
     default: 
      break; 
    } 

    return cell; 
} 

我測試和initWithStyle方法被調用。可以有人幫助我嗎?

+1

HAV您是否嘗試過在你的initWithStyle方法插入的NSLog語句,以確保它獲取調用?你是否在cellForRowAtIndexPath中設置textField文本屬性的值? – jonkroll 2012-03-24 07:48:24

+0

是的,我用NSLog試過。看到我edtited的帖子。 – 2012-03-24 07:53:23

+1

你可以顯示'self.textField'的屬性聲明嗎? – borrrden 2012-03-24 07:55:02

回答

2

如果你有的.xib文件TextFieldCell然後拖放的TextView和設置的所有屬性形成界面生成其他 只能創建* 的.xib通過 *

爲TableViewCell使用

在.h文件中

IBOutlet UITableViewCell * CUSTOM_CELL_XIB;

在.m文件中的cellForRowAtIndexPath方法

static NSString *CellIdentifier = @"Cell"; 


UITableViewCell *cell = (UITableViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

if (cell == nil) 
{ 
    [[NSBundle mainBundle] loadNibNamed:@"CUSTOM_CELL_XIB" owner:self options:nil]; 
    cell = CUSTOM_CELL_XIB; 
} 

,並直接拖放您想文本框或任何UI,我認爲這是自定義單元格創建有效的方式。

這將很好地工作

相關問題