iOS-以编程方式将文本字段添加到uitableviewcell [英] ios - adding text fields to uitableviewcell programmatically not working

查看:76
本文介绍了iOS-以编程方式将文本字段添加到uitableviewcell的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

菜鸟ios问题.我正在尝试使用表格视图控制器创建注册表单.我正在尝试在cellForRowAtIndexPath方法中以编程方式向每个单元格添加文本字段,但是我的所有文本字段似乎都在第一个单元格上创建-一个与另一个单元格重叠.这是我的代码.

Rookie ios question. I am trying to create a sign up form using a table view controller. I am trying add textfields to each cell programmatically in the cellForRowAtIndexPath method but all my text fields seem to getting created on the first cell - one overlapping the other. Here is my code.

这是我的单元格渲染的方式.我认为我在重复使用单元格方面做得很愚蠢.我确实在stackoverflow上检查了一些其他类似的线程,但是它们都没有帮助.你能告诉我我想念的还是这本来应该做的事吗?非常感谢您的投入.

Here is how my cells are rendering. I think I am doing something goofy with the part that is reusing the cells. I did check some some other similar threads on stackoverflow but none of them helped. Could you please tell me what i am missing or is this even the way this is supposed to be done. Your inputs are really appreciated.

谢谢, 迈克

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

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"signUpFields" forIndexPath:indexPath];

// Configure the cell...
if (indexPath.row == 0){
    //self.firstName = [[UITextField alloc] initWithFrame:CGRectMake(5, 0, 280, 21)];
    self.firstName = [[UITextField alloc] initWithFrame:CGRectMake(120, 13, 375, 30)];
    self.firstName.placeholder = @"First Name";
    self.firstName.autocorrectionType = UITextAutocorrectionTypeNo;
    [self.firstName setClearButtonMode:UITextFieldViewModeWhileEditing];
    //cell.accessoryView = self.firstName;
    [cell.contentView addSubview:self.firstName];
}

if (indexPath.row == 1){
    self.lastName = [[UITextField alloc] initWithFrame:CGRectMake(120, 13, 375, 30)];
    self.lastName.placeholder = @"Last Name";
    self.lastName.autocorrectionType = UITextAutocorrectionTypeNo;
    [self.lastName setClearButtonMode:UITextFieldViewModeWhileEditing];
    //cell.accessoryView = self.lastName;
    [cell.contentView addSubview:self.lastName];
}

if (indexPath.row == 2){
    self.emailId = [[UITextField alloc] initWithFrame:CGRectMake(120, 13, 375, 30)];
    self.emailId.placeholder = @"Email Id";
    self.emailId.autocorrectionType = UITextAutocorrectionTypeNo;
    [self.emailId setClearButtonMode:UITextFieldViewModeWhileEditing];
    //cell.accessoryView = self.emailId;
    [cell.contentView addSubview:self.emailId];
}

if (indexPath.row == 3){
    self.password = [[UITextField alloc] initWithFrame:CGRectMake(120, 13, 375, 30)];
    self.password.placeholder = @"Password";
    self.password.secureTextEntry = YES;
    self.password.autocorrectionType = UITextAutocorrectionTypeNo;
    [self.password setClearButtonMode:UITextFieldViewModeWhileEditing];
    //cell.accessoryView = self.password;
    [cell.contentView addSubview:self.password];
}

self.firstName.delegate = self;
self.lastName.delegate = self;
self.emailId.delegate = self;
self.password.delegate = self;

[self.signUpTable addSubview:self.firstName];
[self.signUpTable addSubview:self.lastName];
[self.signUpTable addSubview:self.emailId];
[self.signUpTable addSubview:self.password];

cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;

}

推荐答案

尝试一下..

     - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"Cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }

       if (indexPath.row == 0){
    //self.firstName = [[UITextField alloc] initWithFrame:CGRectMake(5, 0, 280, 21)];
    self.firstName = [[UITextField alloc] initWithFrame:CGRectMake(120, 13, 375, 30)];
    self.firstName.placeholder = @"First Name";
    self.firstName.autocorrectionType = UITextAutocorrectionTypeNo;
    [self.firstName setClearButtonMode:UITextFieldViewModeWhileEditing];
    //cell.accessoryView = self.firstName;
    [cell addSubview:self.firstName];
}

if (indexPath.row == 1){
    self.lastName = [[UITextField alloc] initWithFrame:CGRectMake(120, 13, 375, 30)];
    self.lastName.placeholder = @"Last Name";
    self.lastName.autocorrectionType = UITextAutocorrectionTypeNo;
    [self.lastName setClearButtonMode:UITextFieldViewModeWhileEditing];
    //cell.accessoryView = self.lastName;
    [cell addSubview:self.lastName];
}

if (indexPath.row == 2){
    self.emailId = [[UITextField alloc] initWithFrame:CGRectMake(120, 13, 375, 30)];
    self.emailId.placeholder = @"Email Id";
    self.emailId.autocorrectionType = UITextAutocorrectionTypeNo;
    [self.emailId setClearButtonMode:UITextFieldViewModeWhileEditing];
    //cell.accessoryView = self.emailId;
    [cell addSubview:self.emailId];
}

if (indexPath.row == 3){
    self.password = [[UITextField alloc] initWithFrame:CGRectMake(120, 13, 375, 30)];
    self.password.placeholder = @"Password";
    self.password.secureTextEntry = YES;
    self.password.autocorrectionType = UITextAutocorrectionTypeNo;
    [self.password setClearButtonMode:UITextFieldViewModeWhileEditing];
    //cell.accessoryView = self.password;
    [cell addSubview:self.password];
}

self.firstName.delegate = self;
self.lastName.delegate = self;
self.emailId.delegate = self;
self.password.delegate = self;


cell.selectionStyle = UITableViewCellSelectionStyleNone;

        return cell;
    }

无需在表视图中再次添加文本视图,这是错误的.试试这个,这将为您工作,但更好的是您应该创建一个自定义单元并使用.这是最好的方法,因为您可以在Cell上使用任何您想要的东西.

no need to add again text view in table view, this is wrong. Try this, this will work for you but better you should create a custom Cell and use. That is the best way to do it, because you can use whatever you want with Cell.

这篇关于iOS-以编程方式将文本字段添加到uitableviewcell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆