如何从故事板中将UITextField替换为以编程方式?

时间:2013-06-07 23:17:04

标签: ios uitextfield

我最初创建了一个带有5个tableviewcells的应用程序,其中每个单元格都有自己的图像,一旦单元格中放置了包含占位符文本的uitextfield。当点击该单元格时,它会调用一个uipicker并将值返回到uitextfield。我没有使用cellForRowAtIndexPath方法。

我不得不以编程方式重做单元格的图像视图,因为在敲击单元格时我不得不使用一些新的行为。因此,我必须使用switch case实现cellForRowAtIndexPath以填充每个单元格。我用UIImageViews完成了单元格,但是我很难看到包含UITextField的单元格。

以编程方式创建的uitextfield不显示。这是代码:

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {


    UITableViewCell *cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"MyIdentifier"] autorelease];

    switch (indexPath.row) {
        case 0:
            cell.imageView.image = [UIImage imageNamed:@"Location1.jpg"];
            break;
        case 1:

            break;
        case 2:
            self.myCity = [[UITextField alloc] initWithFrame:CGRectMake(200,6,100,30)];
            self.myCity.text = @"Pick a city...";
            //Call UIPicker
            break;
        case 3:
            cell.imageView.image = [UIImage imageNamed:@"driveThru1.jpg"];
            break;
        case 4:
            cell.imageView.image = [UIImage imageNamed:@"24hrs1.jpg"];
            break;
        case 5:
            break;
        default:
            break;
    }


    return cell;
}

这是故事板:

enter image description here

结果如下:

enter image description here

1 个答案:

答案 0 :(得分:0)

您正在使用新创建的UITextField设置属性myCity。此文本字段实际上从未添加为单元格的子视图。

这是我的建议。创建UITableViewCell的子类,并在其initWithFrame方法中,创建您的UITextField并将其添加为单元格的子视图。确保在cellForRowAtIndexPath中为textfield和non-textfield单元格使用唯一的reuseIdentifers,以便所有内容都正确出列。