iOS - 以编程方式向UITableViewCell添加文本字段不起作用

4
新手ios问题。我正在尝试使用表视图控制器创建注册表单。我正在尝试在cellForRowAtIndexPath方法中以编程方式向每个单元格添加文本字段,但是所有的文本字段似乎都被创建在第一个单元格上 - 一个重叠在另一个上面。以下是我的代码。
这里是我的单元格如何呈现的。我认为我在重用单元格的部分做了些傻事。我确实检查过stackoverflow上的一些其他类似的线程,但它们都没有帮助。请告诉我我漏掉了什么或者这样做的方式是否正确。非常感谢您的意见。
谢谢, 迈克
- (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?并实现prepareForReuse函数来重置单元格数据。更多信息请参阅苹果文档 http://developer.apple.com/library/ios/#documentation/userexperience/conceptual/tableview_iphone/TableViewCells/TableViewCells.html - Jerry Juang
你为什么要把UITextFields添加到tableView上? - Matthias Bauch
8个回答

6
尝试这个...
     - (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;
    }

不需要在表格视图中再次添加文本视图,这是错误的。尝试这个方法,它可以为您工作,但最好创建自定义单元格并使用它。这是最佳方法,因为您可以在单元格中使用任何想要的内容。


4
不要将它们添加为子视图,而是将它们设置为单元格的附属视图。
- (UITextField*)getTextField{
    UITextField *tf = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 20, 35)];
    tf.delegate = self;
    tf.textColor        = [UIColor colorWithRed:.231 green:.337 blue:.533 alpha:1];
    tf.autocorrectionType = UITextAutocorrectionTypeNo;
    tf.borderStyle = UITextBorderStyleNone;
    tf.frame = CGRectMake(0, 20, 170, 30);
    tf.clearButtonMode = UITextFieldViewModeWhileEditing;
    tf.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    tf.font = [UIFont systemFontOfSize:13];
    return tf;
}

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
        cell.textLabel.font = [UIFont systemFontOfSize:13];
        cell.detailTextLabel.font = [UIFont systemFontOfSize:13];
        cell.detailTextLabel.numberOfLines = 2;
    }

    if (indexPath.section == 0) {

        UITextField *tf = (UITextField*)cell.accessoryView;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.textLabel.numberOfLines = 2;
        tf                  = [self getTextField];
        cell.accessoryView = cell.editingAccessoryView = tf;
        [((UITextField*)cell.accessoryView) setBorderStyle:self.tableView.editing ? UITextBorderStyleRoundedRect : UITextBorderStyleNone];
        [((UITextField*)cell.accessoryView) setUserInteractionEnabled:self.tableView.editing ? YES : NO];
        [((UITextField*)cell.accessoryView) setTextAlignment:!self.tableView.editing ? UITextAlignmentRight : UITextAlignmentLeft];
        ((UITextField*)cell.accessoryView).tag = indexPath.row;
    }

    return cell;
}

这个想法是每次出现在屏幕上时重新绘制单元格,从屏幕外部进入,如果你用addSubview:添加文本字段,它也会每次添加。虽然你可以这样做,但需要清除单元格的子视图中contentView,这需要额外的工作、CPU和内存使用,而且并不是最优雅的解决方案。


2

看起来你只想显示四个单元格。这是一个静态情况,单元格不会改变,你应该在xib/storyboard中静态地创建这个tableView和它的单元格。这是首选方式。

如果你真的想以编程方式实现,那么先创建四个带有所需行为的UITableViewCells。将它们保存在数组中。在cellForRowAtIndex path中,return cellArray[indexPath.row];

请注意,这只是因为你只有四个tableViewCells而采用的最佳方法。 只有当你有比屏幕一次容纳更多的单元格时,才会派上重用标识符的用处。所以在你的情况下,你实际上从未重用过单元格。


0

保留这个并尝试它,

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *MyIdentifier = [NSString stringWithFormat:@"%d%d",indexPath.row,indexPath.section];


    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];

    if (cell == nil)
    {
     //your stuff.
    }
}

0

尝试使用这个...而且不需要将这些文本字段添加到表格视图中。所以请删除一些代码...

- (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 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;

}

0

在您跟随我的答案之前,我想告诉您以下代码对于内存管理来说是不好的,因为它将为每行UITableView创建新单元格,所以请小心。

但是,当UITableView有限制的行数(大约50-100行)时,以下代码对您有帮助,请使用它,如果适合您的情况。

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

    NSString *CellIdentifier = [NSString stringWithFormat:@"S%1dR%1d",indexPath.section,indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell == nil)
    {
        cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];


         /// Put/Create  your UITextField or any Controls here


     }

    return cell;
}

0

不要再写更多的单元格,只需创建一个带有文本字段的自定义单元格。为每个文本字段设置标签,并在numberOfRowsIntheSections中设置行数,然后它将显示您需要的单元格数量。我认为这会对您有所帮助。


-1

@Mike:首先我建议您使用StoryBoard或Nib文件。

如果您使用这个,那么您可以使用以下代码

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];

     UITextField *txtField = (UITextField *)[cell viewWithTag:1];
     txtField.text = @"Latest";

     return cell;
}

根据行号您可以设置文本。
如果您想在运行时添加UITextField,则可以使用以下代码。
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
     UITextField *txtField = [[UITextField alloc] initWithFrame:CGRectMake(165.0, 7.0, 150.0, 30.0)];
     txtField.text = @"Latest";
     [cell.contentView addSubview:txtField];
     return cell;
}

你可以分配不同的标签,并根据标签存储这些值。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接