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;

}
4

8 回答 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;
    }

无需在表格视图中再次添加文本视图,这是错误的。试试这个,这对你有用,但更好的是你应该创建一个自定义 Cell 并使用。这是最好的方法,因为您可以随心所欲地使用 Cell。

于 2013-07-17T06:17:44.660 回答
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 和内存使用,并不是说这是最不优雅的解决方案。

于 2013-07-17T06:00:41.520 回答
2

看起来您只有四个要显示的单元格。这是一个静态案例,单元格不会改变,您应该在 xib/storyboard 中静态创建这个 tableView 及其单元格。这是这里的首选方式。

如果您真的想以编程方式执行此操作,请先创建四个具有您想要的行为的 UITableViewCell。将它们保存在一个数组中。在 cellForRowAtIndex 路径内return cellArray[indexPath.row];

请注意,这是最好的方法,因为您只有四个 tableViewCell。仅当您的单元格数量超过屏幕上一次可以容纳的数量时,ReuseIdentifiers 才会派上用场。因此,在您的情况下,您从未真正重用单元格。

于 2013-07-17T06:20:54.900 回答
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.
    }
}
于 2013-07-17T06:01:30.417 回答
0

尝试使用这个......并且没有必要将这些文本字段添加到tableview。所以请删除一些代码..

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

}
于 2013-07-17T06:02:51.080 回答
0

无需编写更多单元格,只需使用文本字段创建一个自定义单元格。为每个文本字段指定标签并在 numberOfRowsIntheSections 中设置行数,然后它将显示您需要多少单元格。我想它会对你有所帮助。

于 2013-07-17T06:03:08.400 回答
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;
}
于 2013-07-17T06:03:53.893 回答
-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;
}

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

于 2013-07-17T06:41:01.843 回答