在我的应用程序中,我使用了一个模态视图控制器,它包含一个带有多个文本字段的TableView。
说我的主视图控制器名为SridharViewController,而模态视图控制器名为ResultsViewController
SridharViewController中有一个按钮,触摸该按钮将打开模态(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];
请为我提供最好的方法。
答案 0 :(得分:2)
在viewWillAppear
中执行以下操作
arrayOfInputs = [NSArray array];
[self.tableview reloadData];
其中arrayOfInputs
是您存储将在tableview上显示的对象的位置。使数据源为空并重新加载表视图后,所有内容都将清除。然后,您可以使用所需数据重新加载表视图。
答案 1 :(得分:0)
如果您想重新加载tableview,请使用以下行
[self.tableview reloadData]
答案 2 :(得分:0)
在ViewDidLoad方法中添加此代码,这将清除所有文本字段文本
for (UIView *view in [self.view subviews]) {
if ([view isKindOfClass:[UITextField class]]) {
UITextField *textField = (UITextField *)view;
textField.text = @"";
}
}
答案 3 :(得分: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;
}