UITableViewCell中的UITextField
问题描述:
我想将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方法被调用。可以有人帮助我吗?
答
如果你有的.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,我认为这是自定义单元格创建有效的方式。
这将很好地工作
HAV您是否尝试过在你的initWithStyle方法插入的NSLog语句,以确保它获取调用?你是否在cellForRowAtIndexPath中设置textField文本属性的值? – jonkroll 2012-03-24 07:48:24
是的,我用NSLog试过。看到我edtited的帖子。 – 2012-03-24 07:53:23
你可以显示'self.textField'的属性声明吗? – borrrden 2012-03-24 07:55:02