0

在我的应用程序中,我使用了一个模态视图控制器,其中包含一个带有多个文本字段的 TableView。

说我的主视图控制器名为 SridharViewController 和模态视图控制器名为 ResultsViewController

SridharViewController 中有一个按钮,触摸该按钮将打开 modal(ResultsViewController)

[self presentModalViewController:resultsViewController animated:YES];

在结果视图控制器中,有许多文本字段在 TableView 中对齐。和一个“确定”按钮。按“确定”按钮将关闭模式

[self dismissModalViewControllerAnimated:YES];

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    //UITextField *formTextField = [[UITextField alloc] initWithFrame:CGRectMake(30, 11, 270, 30)];
    UITextField *formTextField =nil;
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        if(indexPath.row == 0){
            formTextField = [[UITextField alloc] initWithFrame:CGRectMake(30, 12, 270, 30)];
        }
        else{
            formTextField = [[UITextField alloc] initWithFrame:CGRectMake(30, 11, 270, 30)];
        }

    }else{
        if(indexPath.row == 0){
            formTextField = [[UITextField alloc] initWithFrame:CGRectMake(50, 12, 670, 30)];
        }
        else{
            formTextField = [[UITextField alloc] initWithFrame:CGRectMake(50, 11, 670, 30)];
        }

    }


    formTextField.placeholder = [self.formFields objectAtIndex:indexPath.row];
    formTextField.tag = indexPath.row;
    formTextField.text=@"";
    formTextField.adjustsFontSizeToFitWidth = YES;
    formTextField.textColor = [UIColor blackColor];
    formTextField.borderStyle = UITextBorderStyleNone;
    formTextField.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
    [formTextField setAutocapitalizationType:UITextAutocapitalizationTypeNone];
    [formTextField setAutocorrectionType:UITextAutocorrectionTypeNo];
    formTextField.backgroundColor = [UIColor clearColor];
    formTextField.textAlignment = UITextAlignmentLeft;
    formTextField.delegate = self;
    [formTextField setEnabled: YES];
    [formTextField setReturnKeyType:UIReturnKeyNext];
    [self.textFields addObject:formTextField];
    [cell addSubview:formTextField];
    return cell;
}

当我第二次调用模态视图时,模态显示之前的内容。我想清除以前的内容。我已经尝试过更新和重新加载功能。他们都没有调用该cellForRowAtIndexPath方法。

[self.theTable reloadInputViews];

请给我最好的方法来做到这一点。

4

4 回答 4

2

viewWillAppear执行以下操作

arrayOfInputs = [NSArray array];
[self.tableview reloadData];

arrayOfInputs哪里存储将在 tableview 上显示的对象。将数据源清空并重新加载表视图后,所有内容都会清除。然后,您可以使用所需数据重新加载表视图。

于 2013-07-11T04:27:25.607 回答
0

我用这段代码解决了我的问题

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

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        NSLog(@" cellForRowAtIndexPath create new cell %d",indexPath.row);
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
     NSLog(@" cellForRowAtIndexPath update  cell %d",indexPath.row);
    //UITextField *formTextField = [[UITextField alloc] initWithFrame:CGRectMake(30, 11, 270, 30)];
    for(UITextField *lbl in [cell subviews])
    {
          NSLog(@" removing UITextField from  cell %d",indexPath.row);
        if(lbl.tag == (indexPath.row+2)){
             [lbl removeFromSuperview];

        }



    }
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        if(indexPath.row == 0){
            self.tableTextField = [[UITextField alloc] initWithFrame:CGRectMake(30, 12, 270, 30)];
        }
        else{
            self.tableTextField  = [[UITextField alloc] initWithFrame:CGRectMake(30, 11, 270, 30)];
        }

    }else{
        if(indexPath.row == 0){
            self.tableTextField  = [[UITextField alloc] initWithFrame:CGRectMake(50, 12, 670, 30)];
        }
        else{
            self.tableTextField  = [[UITextField alloc] initWithFrame:CGRectMake(50, 11, 670, 30)];
        }

    }


    self.tableTextField .placeholder = [self.formFields objectAtIndex:indexPath.row];
    self.tableTextField .tag = indexPath.row+2;
    self.tableTextField .adjustsFontSizeToFitWidth = YES;
    self.tableTextField .textColor = [UIColor blackColor];
    self.tableTextField .borderStyle = UITextBorderStyleNone;
    self.tableTextField .keyboardType = UIKeyboardTypeNumbersAndPunctuation;
    [self.tableTextField  setAutocapitalizationType:UITextAutocapitalizationTypeNone];
    [self.tableTextField  setAutocorrectionType:UITextAutocorrectionTypeNo];
    self.tableTextField .backgroundColor = [UIColor clearColor];
    self.tableTextField .textAlignment = UITextAlignmentLeft;
    self.tableTextField .delegate = self;
    self.tableTextField .text=@"";
    [self.tableTextField  setEnabled: YES];
    [self.tableTextField  setReturnKeyType:UIReturnKeyNext];
    [cell addSubview:self.tableTextField ];
    return cell;
}
于 2013-07-11T07:42:00.250 回答
0

当您想重新加载表格视图时,请使用以下行

[self.tableview reloadData]
于 2013-07-11T05:23:52.480 回答
0

在 ViewDidLoad 方法中添加此代码,这将清除所有文本字段文本

for (UIView *view in [self.view subviews]) {
        if ([view isKindOfClass:[UITextField class]]) {
            UITextField *textField = (UITextField *)view;

            textField.text = @"";
        }
    }
于 2013-07-11T05:25:54.780 回答