【问题标题】:IOS: Clear Previous content in Modal viewcontrollerIOS:在模态视图控制器中清除以前的内容
【发布时间】:2013-07-11 04:12:50
【问题描述】:

在我的应用程序中,我使用了一个模态视图控制器,它包含一个带有多个文本字段的 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];

请为我提供最好的方法。

【问题讨论】:

  • 以前的内容是什么意思?什么类型的内容? TextField 是否保存他们的文本数据?
  • 在tableview上调用reloadData,然后cellForRowAtIndexPath会被调用。
  • “当我第二次调用模态视图时”——你是怎么做到的?您需要提供更多代码让我们为您提供帮助。
  • @Dilip,我已经编辑了我的问题...
  • 您能否说明您在 tableview 中向 textfeilds 提供数据的位置和方式?因为当您第二次呈现模态视图时,如果您没有保存模态控制器的实例,它实际上会创建新的 tableview 实例。跨度>

标签: iphone ios objective-c uitableview modalviewcontroller


【解决方案1】:

viewWillAppear 中执行以下操作

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

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

【讨论】:

    【解决方案2】:

    当你想重新加载表格视图时,请使用下面的行

    [self.tableview reloadData]
    

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

        【解决方案4】:

        我通过这段代码解决了我的问题

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-10-03
          • 2012-11-18
          • 2012-04-14
          • 2016-02-03
          • 1970-01-01
          • 1970-01-01
          • 2014-08-03
          • 2014-04-06
          相关资源
          最近更新 更多