如何在uitableview中添加子视图作为弹出窗口?

时间:2015-10-13 12:06:35

标签: ios objective-c uitableview uialertview uipopovercontroller

当用户选择单元格时,我有一个表格视图,我需要显示我的子视图,是否可能?

我试过这个:

  1. 我创建了子视图。
  2. 最初我已将其声明为hidden = yes;
  3. 当用户选择单元格时,我已经为我的子视图提供了hidden = no 但没有任何事情发生.....
  4. 任何人请帮我找出解决方案。
  5. 我使用的代码;

    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        [resheduleView bringSubviewToFront:self.view];
    
        resheduleView.hidden=NO;
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"PopUP Title"
                                                        message:@"This is pop up window/ Alert"
                                                       delegate:nil
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil];
    
    //    UIAlertView *alert=[[UIAlertView alloc]init];
    
        [alert addSubview:resheduleView];
    
    
    
    
    }
    

2 个答案:

答案 0 :(得分:0)

如果您已在表格视图单元格中添加了子视图,请尝试以下代码。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

      UITableViewCell *cell1 = [tableView cellForRowAtIndexPath:indexPath];
      cell1.YOUR_SUB_VIEW.hidden = NO;
}

OR

   - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

      UITableViewCell *cell1 = [tableView cellForRowAtIndexPath:indexPath];
      UIView *objViewTemp = cell1.YOUR_SUB_VIEW; //Create object of view which you have used
      objViewTemp.hidden = NO;
}

谢谢:)

答案 1 :(得分:0)

以下是您的要求的示例。试试这个

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 5;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell=[[UITableViewCell alloc]init];

    cell.textLabel.text=[NSString stringWithFormat:@"cell : %ld",(long)indexPath.row];

    return cell;

}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    UITableViewCell *cell=[tableView cellForRowAtIndexPath:indexPath];


    UIView *vwTemp=[self.view viewWithTag:987654];
    [vwTemp removeFromSuperview];
    vwTemp=nil;

    UIView *vwPopUp=[[UIView alloc]init];
    vwPopUp.tag=987654;
    [vwPopUp setFrame:CGRectMake(15, 100, 290, 300)];
    [vwPopUp setBackgroundColor:[UIColor blackColor]];


    UILabel *lbl=[[UILabel alloc]initWithFrame:CGRectMake(0, 50, 290, 30)];
    [lbl setTextAlignment:NSTextAlignmentCenter];
    [lbl setTextColor:[UIColor whiteColor]];
    lbl.text=[NSString stringWithFormat:@"You have selected : %@",cell.textLabel.text];
    [vwPopUp addSubview:lbl];

    [self.view addSubview:vwPopUp];



}


-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    UIView *vwTemp=[self.view viewWithTag:987654];
    [vwTemp removeFromSuperview];
    vwTemp=nil;
}