Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/108.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 重复UITextField';在tableView滚动时添加的_Ios_Objective C_Uitableview_Uitextfield - Fatal编程技术网

Ios 重复UITextField';在tableView滚动时添加的

Ios 重复UITextField';在tableView滚动时添加的,ios,objective-c,uitableview,uitextfield,Ios,Objective C,Uitableview,Uitextfield,我遇到了一个问题,重复使用单元格时会添加重复的UITextField,这不是我想要的。我记得以前遇到过类似的问题,但我一辈子都记不起我是怎么解决的 毫无疑问,这是显而易见的,但我似乎找不到任何有用的东西。我曾尝试按照一些人的建议将if/else语句封装在“if(cell==null)”中,但这只会形成一个空白表 如果您能提供一些建议,我们将不胜感激 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIn

我遇到了一个问题,重复使用单元格时会添加重复的UITextField,这不是我想要的。我记得以前遇到过类似的问题,但我一辈子都记不起我是怎么解决的

毫无疑问,这是显而易见的,但我似乎找不到任何有用的东西。我曾尝试按照一些人的建议将if/else语句封装在“if(cell==null)”中,但这只会形成一个空白表

如果您能提供一些建议,我们将不胜感激

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


        if (indexPath.section == 0){

            productField = [[UITextField alloc] initWithFrame:CGRectMake(10, 0, 300, 45)];
            [productField setPlaceholder:@"Product"];
            if ([productData length] > 0){
                [productField setText:productData];
                [productField setEnabled:false];
            }
            [cell addSubview:productField];

        } else if (indexPath.section == 1){

            issueField = [[UITextField alloc] initWithFrame:CGRectMake(10, 0, 300, 45)];
            [issueField setPlaceholder:@"What's the issue?"];
            [issueField setAutocorrectionType:UITextAutocorrectionTypeNo];
            [cell addSubview:issueField];

        } else if (indexPath.section == 2){

            emailField = [[UITextField alloc] initWithFrame:CGRectMake(10, 0, 300, 45)];
            [emailField setPlaceholder:@"Email address"];
            [emailField setAutocorrectionType:UITextAutocorrectionTypeNo];
            [emailField setAutocapitalizationType:UITextAutocapitalizationTypeNone];
            [emailField setKeyboardType:UIKeyboardTypeEmailAddress];
            [cell addSubview:emailField];

        } else if (indexPath.section == 3){

            notesField = [[UITextField alloc] initWithFrame:CGRectMake(10, 0, 300, 45)];
            [notesField setPlaceholder:@"Any notes to add?"];
            [notesField setAutocorrectionType:UITextAutocorrectionTypeNo];
            [cell addSubview:notesField];

        } else if (indexPath.section == 4){

            sendFeedback = [[UIButton alloc] initWithFrame:CGRectMake(10, 0, 300, 45)];
            [sendFeedback setTitle:@"Send Feedback" forState:UIControlStateNormal];
            [sendFeedback setBackgroundColor:[UIColor colorWithRed:(111/255.0f) green:(31/255.0f) blue:(68/255.0f) alpha:1.0f]];
            [sendFeedback.titleLabel setFont:[UIFont fontWithName:@"MaryAnn" size:20.0]];
            [sendFeedback addTarget:self action:@selector(sendFeedback:) forControlEvents:UIControlEventTouchUpInside];
            [cell setBackgroundColor:[UIColor clearColor]];
            [cell addSubview:sendFeedback];

        }


    return cell;
}

这是因为您不断将子视图添加到单元格中。例如,将
productField
添加到索引0处的单元格中。当该单元格被重用时,您可以向其添加另一个子视图,但
productField
仍将存在。一个可能的解决方案是在添加新的子视图之前删除该子视图。您的
if
语句如下:

    if (indexPath.section == 0){
        // Remove the old subview
        UIView *oldSubview = [cell viewWithTag:5];
        [oldSubview removeFromSuperview];

        productField = [[UITextField alloc] initWithFrame:CGRectMake(10, 0, 300, 45)];
        [productField setPlaceholder:@"Product"];
        if ([productData length] > 0){
            [productField setText:productData];
            [productField setEnabled:false];
        }
        // Set a tag for the new subview, so you can remove it later
        productField.tag = 5;
        [cell addSubview:productField];
    }
在每个
if
语句中使用相同的
标记

编辑:


或者你可以像@pawan建议的那样使用静态单元格。

问题是,既然你在回收单元格(你应该这样做),你就在重复使用非空单元格,并在其上添加新的控件

通常的处理方法是构建自定义UITableViewCell子类,并将各种文本字段和其他UI控件作为属性。 然后可以在每个部分中显示/隐藏这些UI控件

看起来是这样的:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
        if (indexPath.section == 0){
            productField.hidden=NO;
            issueField.hidden=YES;

        } else if (indexPath.section == 1){
            productField.hidden=YES;
            issueField.hidden=NO;
        }
return cell;
}

为什么不在实例化单元后,从单元contentView中删除所有子视图

for( UIView *view in cell.contentView.subviews )
    [view removeFromSuperview];

您的问题是表视图中的可重用单元格,请尝试以下代码

- (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];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        UITextField* textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 0, 300, 45)];
        textField.tag=101;
        [cell addSubview:textField];

        UIButton *sendFeedback = [[UIButton alloc] initWithFrame:CGRectMake(10, 0, 300, 45)];
        sendFeedback.tag=102;
        [sendFeedback setTitle:@"Send Feedback" forState:UIControlStateNormal];
        [sendFeedback setBackgroundColor:[UIColor colorWithRed:(111/255.0f) green:(31/255.0f) blue:(68/255.0f) alpha:1.0f]];
        [sendFeedback.titleLabel setFont:[UIFont fontWithName:@"MaryAnn" size:20.0]];
        [sendFeedback addTarget:self action:@selector(sendFeedback:) forControlEvents:UIControlEventTouchUpInside];
        [cell setBackgroundColor:[UIColor clearColor]];
        [cell addSubview:sendFeedback];

    }
    UITextField* textField=(UITextField*)[cell viewWithTag:101];
    UIButton *sendFeedback = (UIButton*)[cell viewWithTag:102];
    sendFeedback.hidden=YES;
    textField.hidden=NO;
     [textField setKeyboardType:UIKeyboardTypeDefault];
    if (indexPath.section == 0)
    {
        [textField setPlaceholder:@"Product"];
        if ([productData length] > 0)
        {
            [textField setText:productData];
            [textField setEnabled:false];
        }

    } else if (indexPath.section == 1){

        [textField setPlaceholder:@"What's the issue?"];
        [textField setAutocorrectionType:UITextAutocorrectionTypeNo];

    } else if (indexPath.section == 2){

        [textField setPlaceholder:@"Email address"];
        [textField setAutocorrectionType:UITextAutocorrectionTypeNo];
        [textField setAutocapitalizationType:UITextAutocapitalizationTypeNone];
        [textField setKeyboardType:UIKeyboardTypeEmailAddress];

    } else if (indexPath.section == 3){

        [textField setPlaceholder:@"Any notes to add?"];
        [textField setAutocorrectionType:UITextAutocorrectionTypeNo];

    } else if (indexPath.section == 4)
    {
        textField.hidden=YES;
        sendFeedback.hidden=NO;
    }

    return cell;
}

//您不能在表视图中直接访问textField.text,您可以使用全局变量或字典分配textField.text

您应该使用静态单元格来代替dequeueReusableCell。您不能创建自定义单元格吗?谢谢帮助。我选择了静态单元(完全忘记了它们),因为它只会在测试版本中使用,而不会在应用程序的生产版本中使用。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *CellIdentifier = [NSString stringWithFormat:@"Cell %d",indexPath.section];

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    cell = nil;
    if (cell == nil)
    {

    if (indexPath.section == 0)
    {

        productField = [[UITextField alloc] initWithFrame:CGRectMake(10, 0, 300, 45)];
        [productField setPlaceholder:@"Product"];
        if ([productData length] > 0){
            [productField setText:productData];
            [productField setEnabled:false];
        }
        [cell addSubview:productField];

    }
    else if (indexPath.section == 1)
    {

        issueField = [[UITextField alloc] initWithFrame:CGRectMake(10, 0, 300, 45)];
        [issueField setPlaceholder:@"What's the issue?"];
        [issueField setAutocorrectionType:UITextAutocorrectionTypeNo];
        [cell addSubview:issueField];

    } else if (indexPath.section == 2){

        emailField = [[UITextField alloc] initWithFrame:CGRectMake(10, 0, 300, 45)];
        [emailField setPlaceholder:@"Email address"];
        [emailField setAutocorrectionType:UITextAutocorrectionTypeNo];
        [emailField setAutocapitalizationType:UITextAutocapitalizationTypeNone];
        [emailField setKeyboardType:UIKeyboardTypeEmailAddress];
        [cell addSubview:emailField];

    } else if (indexPath.section == 3){

        notesField = [[UITextField alloc] initWithFrame:CGRectMake(10, 0, 300, 45)];
        [notesField setPlaceholder:@"Any notes to add?"];
        [notesField setAutocorrectionType:UITextAutocorrectionTypeNo];
        [cell addSubview:notesField];

    } else if (indexPath.section == 4){

        sendFeedback = [[UIButton alloc] initWithFrame:CGRectMake(10, 0, 300, 45)];
        [sendFeedback setTitle:@"Send Feedback" forState:UIControlStateNormal];
        [sendFeedback setBackgroundColor:[UIColor colorWithRed:(111/255.0f) green:(31/255.0f) blue:(68/255.0f) alpha:1.0f]];
        [sendFeedback.titleLabel setFont:[UIFont fontWithName:@"MaryAnn" size:20.0]];
        [sendFeedback addTarget:self action:@selector(sendFeedback:) forControlEvents:UIControlEventTouchUpInside];
        [cell setBackgroundColor:[UIColor clearColor]];
        [cell addSubview:sendFeedback];

    }

    }
    return cell;
}