-2

I am creating a view on UITableView here is the code

UIView *olderLinesView = (UIView*)[self.view viewWithTag:1820];

for (UIView* view in [olderLinesView subviews]) {
    [view removeFromSuperview];
}
[olderLinesView removeFromSuperview];

UIView *linesView=[[UIView alloc]initWithFrame:CGRectMake(2, -2, 1022, height)];
[linesView setBackgroundColor:[UIColor clearColor]];
[linesView setUserInteractionEnabled:NO];
[linesView setTag:1820];
[tableView addSubview:linesView];

But the problem is if i remove olderLinesView before creating linesView (same view) the linesView is not created at all. And if i don't remove this view before creating it, it redraws the view over and over. I am calling this code again and again so need to remove this view before creating its new instance. What i am doing wrong here?

2
  • Are you shure that linesView is nil, or the view is not added to the tableView ? Sep 3, 2014 at 6:34
  • It is not being displayed. It suddenly hides, when i checked the object of linesView was not nil it is created but not being displayed
    – Hassy
    Sep 3, 2014 at 6:39

1 Answer 1

1

You need to modify code as below

UIView *olderLinesView = (UIView*)[self.view viewWithTag:1820];

for (UIView* view in [tableview subviews]) 
{
      if( [view isKindOfClass:[olderLinesView class]] )
    {
      for (UIView* subview in [view subviews]) 
      {

      [subview removeFromSuperview]; //if you want all views
      }
    }
}
///(OR)

for (UIView* view in [tableview subviews]) 
{
      if( [view isKindOfClass:[olderLinesView class]] )
    {
      for (UIView* subview in [view subviews]) 
      {
      if(subview.tag==1820)
      {

        [subview removeFromSuperview];//if you want remove single view
      }
    }
}

Hope it helps you...!

1
  • I was waiting for the time until you can accept answer :)
    – Hassy
    Sep 3, 2014 at 6:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.