gpt4 book ai didi

iphone - UITableViewCell 中的 UITextField

转载 作者:行者123 更新时间:2023-11-29 11:15:02 25 4
gpt4 key购买 nike

我想将 UItextField 放在 UITableViewCell 中,但我遇到了问题。文本字段不出现。这是我的代码:

-(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 并设置 Interface builder else 的所有属性您只能创建 *.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;
}

并直接拖放 textField 或您想要的任何 UI,我认为这是创建自定义单元格的有效方式。

这将完美地工作

关于iphone - UITableViewCell 中的 UITextField,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9849987/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com