0

我在我的 iPad 应用程序中使用一张桌子和一个 UIview。我想在表的选定行索引的第二个视图中显示不同的选项。应用程序 UI 附有此。我的代码片段是这样的: -

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSUInteger row=indexPath.row;

UILabel *lblName, *lblUserName, *lblOldPassword, *lblCreditCardDetails, *lblNewPassword;

UITextField *txtName, *txtUserName, *txtOldPassword, *txtNewPassword, *txtCreditCardDetails;

UIButton *btnsaveprofile, *btnSavePassword, *btnSaveCreditCardDetails;

UIView *subView =[[UIView alloc] initWithFrame:CGRectMake(321, 0, 448, 980)];
[viewDetail addSubview:subView];

if (row==0) {

    [subView removeFromSuperview];

    UIView *subView =[[UIView alloc] initWithFrame:CGRectMake(0, 0, 448, 980)];
    [viewDetail addSubview:subView];

    lblName =[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 140, 40)];
    lblName.text=@"Name";
    [lblName setBackgroundColor:[UIColor clearColor]];
    [subView addSubview:lblName];
    [lblName release];

    txtName =[[UITextField alloc] initWithFrame:CGRectMake(141, 0, 200, 40)];
    txtName.borderStyle=UITextBorderStyleRoundedRect;
    [subView addSubview:txtName];

    lblUserName =[[UILabel alloc] initWithFrame:CGRectMake(0, 60, 140, 40)];
    lblUserName.text=@"User Name";
    [lblUserName setBackgroundColor:[UIColor clearColor]];
    [subView addSubview:lblUserName];
    [lblUserName release];


    txtUserName =[[UITextField alloc] initWithFrame:CGRectMake(141, 60, 200, 40)];
    txtUserName.borderStyle=UITextBorderStyleRoundedRect;
    [subView addSubview:txtUserName];


    btnsaveprofile =[[UIButton alloc] initWithFrame:CGRectMake(100, 160, 100, 40)];
    [btnsaveprofile setTitle:@"Save" forState:UIControlStateNormal];
    [btnsaveprofile addTarget:self action:@selector(btnSaveProfile) forControlEvents:UIControlEventTouchUpInside];
    [subView addSubview:btnsaveprofile];


}        
if (row==1) {

   [subView removeFromSuperview];

    UIView *subView1 =[[UIView alloc] initWithFrame:CGRectMake(0, 0, 448, 980)];
    [viewDetail addSubview:subView1];

    lblOldPassword =[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 140, 40)];
    lblOldPassword.text=@"Old Password";
    [lblOldPassword setBackgroundColor:[UIColor clearColor]];
    [subView1 addSubview:lblOldPassword];


    txtOldPassword =[[UITextField alloc] initWithFrame:CGRectMake(141, 0, 200, 40)];
    txtOldPassword.secureTextEntry=YES;
    txtOldPassword.borderStyle=UITextBorderStyleRoundedRect;
    [subView1 addSubview:txtOldPassword];


    lblNewPassword =[[UILabel alloc] initWithFrame:CGRectMake(0, 60, 140, 40)];
    lblNewPassword.text=@"New Password";
    [lblNewPassword setBackgroundColor:[UIColor clearColor]];
    [subView1 addSubview:lblNewPassword];


    txtNewPassword =[[UITextField alloc] initWithFrame:CGRectMake(141, 60, 200, 40)];
    txtNewPassword.secureTextEntry=YES;
    txtNewPassword.borderStyle=UITextBorderStyleRoundedRect;
    [subView1 addSubview:txtNewPassword];


    btnSavePassword =[[UIButton alloc] initWithFrame:CGRectMake(100, 160, 100, 40)];
    [btnSavePassword setTitle:@"Save" forState:UIControlStateNormal];
    [btnSavePassword addTarget:self action:@selector(btnNavigationBarClicked) forControlEvents:UIControlEventTouchUpInside];
    [btnSavePassword setBackgroundImage:[UIImage imageNamed:@"btn_signin_bg.png"] forState:UIControlStateNormal];
    [subView1 addSubview:btnSavePassword];


}
if (row==2) {

    [subView removeFromSuperview];


    UIView *subView =[[UIView alloc] initWithFrame:CGRectMake(0, 0, 448, 980)];
    [viewDetail addSubview:subView];

    lblCreditCardDetails =[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 180, 40)];
    lblCreditCardDetails.text=@"Credit Card Details";
    [lblCreditCardDetails setBackgroundColor:[UIColor clearColor]];
    [subView addSubview:lblCreditCardDetails];
    [lblCreditCardDetails release];

    txtCreditCardDetails =[[UITextField alloc] initWithFrame:CGRectMake(181, 0, 200, 40)];
    txtCreditCardDetails.secureTextEntry=YES;
    txtCreditCardDetails.borderStyle=UITextBorderStyleRoundedRect;
    [subView addSubview:txtCreditCardDetails];
    [txtCreditCardDetails release];


    btnSaveCreditCardDetails =[[UIButton alloc] initWithFrame:CGRectMake(100, 130, 100, 40)];
    [btnSaveCreditCardDetails setTitle:@"Save" forState:UIControlStateNormal];
    [btnSaveCreditCardDetails setBackgroundImage:[UIImage imageNamed:@"btn_signin_bg.png"] forState:UIControlStateNormal];
    [btnSaveCreditCardDetails addTarget:self action:@selector(btnSaveCreditCard) forControlEvents:UIControlEventTouchUpInside];
    [subView addSubview:btnSaveCreditCardDetails];
    [btnSaveCreditCardDetails release];
    btnSaveCreditCardDetails.hidden=YES;

}

}

我正在使用此代码,但不幸的是它与以前的视图重叠。

我的问题是如何避免重叠或有什么方法可以隐藏以前的视图。

在此处输入图像描述

4

2 回答 2

1

我已经编辑了您的代码,您需要在添加其他视图之前删除 detailView 的所有子视图,因此只需使用以下代码:

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSUInteger row=indexPath.row;
UILabel *lblName, *lblUserName, *lblOldPassword, *lblCreditCardDetails, *lblNewPassword;
UITextField *txtName, *txtUserName, *txtOldPassword, *txtNewPassword, *txtCreditCardDetails;
UIButton *btnsaveprofile, *btnSavePassword, *btnSaveCreditCardDetails;
//UIView *subView =[[UIView alloc] initWithFrame:CGRectMake(321, 0, 448, 980)];
//[viewDetail addSubview:subView];

//Remove all subviews of viewDetail

  for (UIView *subviews in viewDetail.subviews)
    {
        [subviews removeFromSuperview];
    }

if (row==0) {
//    [subView removeFromSuperview];
    UIView *subView =[[UIView alloc] initWithFrame:CGRectMake(0, 0, 448, 980)];
    [viewDetail addSubview:subView];
    lblName =[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 140, 40)];
    lblName.text=@"Name";
    [lblName setBackgroundColor:[UIColor clearColor]];
    [subView addSubview:lblName];
    [lblName release];
    txtName =[[UITextField alloc] initWithFrame:CGRectMake(141, 0, 200, 40)];
    txtName.borderStyle=UITextBorderStyleRoundedRect;
    [subView addSubview:txtName];
    lblUserName =[[UILabel alloc] initWithFrame:CGRectMake(0, 60, 140, 40)];
    lblUserName.text=@"User Name";
    [lblUserName setBackgroundColor:[UIColor clearColor]];
    [subView addSubview:lblUserName];
    [lblUserName release];
    txtUserName =[[UITextField alloc] initWithFrame:CGRectMake(141, 60, 200, 40)];
    txtUserName.borderStyle=UITextBorderStyleRoundedRect;
    [subView addSubview:txtUserName];
    btnsaveprofile =[[UIButton alloc] initWithFrame:CGRectMake(100, 160, 100, 40)];
    [btnsaveprofile setTitle:@"Save" forState:UIControlStateNormal];
    [btnsaveprofile addTarget:self action:@selector(btnSaveProfile) forControlEvents:UIControlEventTouchUpInside];
    [subView addSubview:btnsaveprofile];
}        
if (row==1) {
//   [subView removeFromSuperview];
    UIView *subView1 =[[UIView alloc] initWithFrame:CGRectMake(0, 0, 448, 980)];
    [viewDetail addSubview:subView1];
    lblOldPassword =[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 140, 40)];
    lblOldPassword.text=@"Old Password";
    [lblOldPassword setBackgroundColor:[UIColor clearColor]];
    [subView1 addSubview:lblOldPassword];
    txtOldPassword =[[UITextField alloc] initWithFrame:CGRectMake(141, 0, 200, 40)];
    txtOldPassword.secureTextEntry=YES;
    txtOldPassword.borderStyle=UITextBorderStyleRoundedRect;
    [subView1 addSubview:txtOldPassword];
    lblNewPassword =[[UILabel alloc] initWithFrame:CGRectMake(0, 60, 140, 40)];
    lblNewPassword.text=@"New Password";
    [lblNewPassword setBackgroundColor:[UIColor clearColor]];
    [subView1 addSubview:lblNewPassword];
    txtNewPassword =[[UITextField alloc] initWithFrame:CGRectMake(141, 60, 200, 40)];
    txtNewPassword.secureTextEntry=YES;
    txtNewPassword.borderStyle=UITextBorderStyleRoundedRect;
    [subView1 addSubview:txtNewPassword];
    btnSavePassword =[[UIButton alloc] initWithFrame:CGRectMake(100, 160, 100, 40)];
    [btnSavePassword setTitle:@"Save" forState:UIControlStateNormal];
    [btnSavePassword addTarget:self action:@selector(btnNavigationBarClicked) forControlEvents:UIControlEventTouchUpInside];
    [btnSavePassword setBackgroundImage:[UIImage imageNamed:@"btn_signin_bg.png"] forState:UIControlStateNormal];
    [subView1 addSubview:btnSavePassword];
}
if (row==2) {
 //   [subView removeFromSuperview];
    UIView *subView =[[UIView alloc] initWithFrame:CGRectMake(0, 0, 448, 980)];
    [viewDetail addSubview:subView];
    lblCreditCardDetails =[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 180, 40)];
    lblCreditCardDetails.text=@"Credit Card Details";
    [lblCreditCardDetails setBackgroundColor:[UIColor clearColor]];
    [subView addSubview:lblCreditCardDetails];
    [lblCreditCardDetails release];
    txtCreditCardDetails =[[UITextField alloc] initWithFrame:CGRectMake(181, 0, 200, 40)];
    txtCreditCardDetails.secureTextEntry=YES;
    txtCreditCardDetails.borderStyle=UITextBorderStyleRoundedRect;
    [subView addSubview:txtCreditCardDetails];
    [txtCreditCardDetails release];
    btnSaveCreditCardDetails =[[UIButton alloc] initWithFrame:CGRectMake(100, 130, 100, 40)];
    [btnSaveCreditCardDetails setTitle:@"Save" forState:UIControlStateNormal];
    [btnSaveCreditCardDetails setBackgroundImage:[UIImage imageNamed:@"btn_signin_bg.png"] forState:UIControlStateNormal];
    [btnSaveCreditCardDetails addTarget:self action:@selector(btnSaveCreditCard) forControlEvents:UIControlEventTouchUpInside];
    [subView addSubview:btnSaveCreditCardDetails];
    [btnSaveCreditCardDetails release];
    btnSaveCreditCardDetails.hidden=YES;
}
于 2013-05-02T09:41:08.353 回答
0

试试下面的代码而不是 [subView removeFromSuperview];

[[viewDetail subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];
于 2013-05-02T09:33:52.200 回答