redribben

accordianview

Jan 30th, 2015
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. //  AR_ViewController.m
  3. //  AccordianView
  4. //
  5. //
  6.  
  7. #import "AR_ViewController.h"
  8. #define Yvalueinitial 0
  9. @interface AR_ViewController ()
  10. @property (weak, nonatomic) IBOutlet UIScrollView *scrollViewForAccordian;
  11.  
  12. @property (nonatomic) BOOL isExpanded;
  13. @property (nonatomic) NSUInteger tagOfViewExpanded;
  14.  
  15. @end
  16.  
  17. @implementation AR_ViewController
  18. @synthesize scrollViewForAccordian;
  19. @synthesize isExpanded;
  20. @synthesize tagOfViewExpanded;
  21.  
  22. - (void)viewDidLoad {
  23.     [super viewDidLoad];
  24.     // Do any additional setup after loading the view, typically from a nib.
  25.     [self settingDifferentDevices];
  26.     [self setTableOuter:17];
  27. }
  28.  
  29. - (void)didReceiveMemoryWarning {
  30.     [super didReceiveMemoryWarning];
  31.     // Dispose of any resources that can be recreated.
  32. }
  33.  
  34. -(void) settingDifferentDevices {
  35.     CGRect screenBounds = [[UIScreen mainScreen] bounds];
  36.    
  37.     if (screenBounds.size.height == 480) {
  38.         NSLog(@"its 3.5 inch");
  39.         scrollViewForAccordian.frame  = CGRectMake(scrollViewForAccordian.frame.origin.x, scrollViewForAccordian.frame.origin.y, scrollViewForAccordian.frame.size.width, scrollViewForAccordian.frame.size.height-88);
  40.     }
  41.     else {
  42.         NSLog(@"its 4 inch");
  43.     }
  44. }
  45.  
  46. -(void)setTableOuter:(NSUInteger)noOfOuterRows
  47. {
  48.     int yValue=Yvalueinitial;
  49.     for (UIView *views in scrollViewForAccordian.subviews) {
  50.         [views removeFromSuperview];
  51.     }
  52.     for (int i=1 ; i<= noOfOuterRows; i++) {
  53.         //Making the Cell
  54.         UIView *viewForOutertable = [[UIView alloc]initWithFrame:CGRectMake(0, yValue, 320, 44)];
  55.         viewForOutertable.tag = i;
  56.        
  57.         //Labeling each cell
  58.         UILabel *labelOnOuter = [[UILabel alloc]initWithFrame:CGRectMake(60, 7, 250, 30)];
  59.         labelOnOuter.text = [NSString stringWithFormat:@"Process no: %i", i];
  60.         labelOnOuter.textColor = [UIColor whiteColor];
  61.        
  62.         //Setting the buttons
  63.         UIButton *buttonToEnlarge = [[UIButton alloc]initWithFrame:CGRectMake(20, 7, 30 , 30)];
  64.         buttonToEnlarge.tag = i;
  65.         [buttonToEnlarge setImage:[UIImage imageNamed:@"open.png"]
  66.                          forState:UIControlStateNormal];
  67.        
  68.         [buttonToEnlarge setImage:[UIImage imageNamed:@"close.png"]
  69.                          forState:UIControlStateSelected];
  70.         [buttonToEnlarge addTarget:self
  71.                             action:@selector(expandButtonTapped:)
  72.                   forControlEvents: UIControlEventTouchUpInside];
  73.        
  74.        
  75.         UIImageView *imageViewFOrOuterTable = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
  76.         imageViewFOrOuterTable.image = [UIImage imageNamed:@"ltgrey.jpg"];
  77.        
  78.         [viewForOutertable addSubview:imageViewFOrOuterTable];
  79.         [viewForOutertable addSubview:labelOnOuter];
  80.         [viewForOutertable addSubview:buttonToEnlarge];
  81.         [scrollViewForAccordian addSubview:viewForOutertable];
  82.         yValue = yValue+viewForOutertable.frame.size.height;
  83.     }
  84.     [scrollViewForAccordian setContentSize:CGSizeMake(320, yValue)];
  85. }
  86.  
  87.  
  88.  
  89. -(void)setTableInner:(NSUInteger)noOfInnerRows forTag:(NSUInteger)tagForView {
  90.     float yValue=Yvalueinitial, yValueForSubviews;
  91.     tagOfViewExpanded = tagForView;
  92.     UIView *viewBelowWhichItStarts;
  93.     NSMutableArray *subviewsArray = [scrollViewForAccordian.subviews mutableCopy];
  94.     NSSortDescriptor *aSortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"tag" ascending:YES];
  95.     [subviewsArray sortUsingDescriptors:[NSArray arrayWithObject:aSortDescriptor]];
  96.    
  97.     for (UIView * subviews in subviewsArray) {
  98.         if (subviews.tag == 0 ) {
  99.             [subviews removeFromSuperview];
  100.         }
  101.         if (subviews.tag ==tagForView) {
  102.             viewBelowWhichItStarts = subviews;
  103.             yValueForSubviews=viewBelowWhichItStarts.frame.origin.y+viewBelowWhichItStarts.frame.size.height;
  104.         }
  105.     }
  106.     yValue = viewBelowWhichItStarts.frame.origin.y+44;
  107.     for (int i=0 ; i< noOfInnerRows; i++) {
  108.         UIView *viewForInnerTable = [[UIView alloc]initWithFrame:CGRectMake(0, yValue, 320, 44)];
  109.         viewForInnerTable.backgroundColor = [UIColor redColor];
  110.         yValue = yValue+viewForInnerTable.frame.size.height;
  111.         UIImageView *imageViewForInnerTable = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
  112.         imageViewForInnerTable.image = [UIImage imageNamed:@"light_gray_bar_with_blue_stripe.png"];
  113.         [viewForInnerTable addSubview:imageViewForInnerTable];
  114.        
  115.         UILabel *labelForInner = [[UILabel alloc]initWithFrame:CGRectMake(50, 7, 200, 30)];
  116.         float i = arc4random_uniform(8);
  117.         labelForInner.text= [NSString stringWithFormat:@"subProcess: %i", (int)i];
  118.         [viewForInnerTable addSubview:labelForInner];
  119.         [scrollViewForAccordian addSubview:viewForInnerTable];
  120.     }
  121.     yValueForSubviews = yValue;
  122.     for (UIView * subviews in subviewsArray) {
  123.         if (subviews.tag > tagForView) {
  124.             [scrollViewForAccordian bringSubviewToFront:subviews];
  125.             [UIView beginAnimations:nil context:NULL];
  126.             [UIView setAnimationDuration:0.3];
  127.             subviews.frame = CGRectMake(0, yValueForSubviews, 320, viewBelowWhichItStarts.frame.size.height);
  128.             [UIView commitAnimations];
  129.            
  130.             yValueForSubviews = yValueForSubviews + viewBelowWhichItStarts.frame.size.height;
  131.         }
  132.     }
  133.     [scrollViewForAccordian setContentSize:CGSizeMake(320, yValueForSubviews+2)];
  134. }
  135.  
  136.  
  137. -(void)expandButtonTapped:(UIButton *)sender {
  138.     for (UIView *outerViews in scrollViewForAccordian.subviews) {
  139.         if (outerViews.tag >0) {
  140.             for (UIView *subviews in outerViews.subviews) {
  141.                 if ([subviews isMemberOfClass:[UIButton class]]) {
  142.                     ((UIButton *)subviews).selected = NO;
  143.                 }
  144.                 if ([subviews isMemberOfClass:[UIImageView class]]) {
  145.                     ((UIImageView *)subviews).image = [UIImage imageNamed:@"ltgrey.jpg"];
  146.                     if (outerViews.tag == sender.tag && !sender.selected) {
  147.                         ((UIImageView *)subviews).image = [UIImage imageNamed:@"grey.jpg"];
  148.                     }
  149.                 }
  150.             }
  151.         }
  152.     }
  153.     if (!isExpanded) { // when NO process tree is already expanded
  154.         isExpanded = YES;
  155.         sender.selected = YES;
  156.         [self setTableInner:sender.tag forTag:sender.tag];
  157.     }
  158.     else // { when the process tree is already expanded
  159.         isExpanded =NO;
  160.         if (sender.tag == tagOfViewExpanded) {
  161.             sender.selected = NO;
  162.             [self bringUpViews:tagOfViewExpanded];
  163.         }
  164.         else {
  165.             sender.selected = YES;
  166.             [self bringUpViews:tagOfViewExpanded];
  167.             [self setTableInner:sender.tag forTag:sender.tag];
  168.             isExpanded = YES;
  169.         }
  170.        
  171.     }
  172.  
  173.  
  174. -(void)bringUpViews:(NSUInteger)tagForView {
  175.    
  176.     UIView *viewBelowWhichItStarts;
  177.     float yValueForSubviews;
  178.     for (UIView * subviews in scrollViewForAccordian.subviews) {
  179.         if (subviews.tag == tagForView) {
  180.             viewBelowWhichItStarts =subviews;
  181.             yValueForSubviews = viewBelowWhichItStarts.frame.origin.y+44;
  182.         }
  183.         if (subviews.tag > tagForView ) {
  184.             [UIView beginAnimations:nil context:NULL];
  185.             [UIView setAnimationDuration:0.3];
  186.             subviews.frame = CGRectMake(0, yValueForSubviews, 320, viewBelowWhichItStarts.frame.size.height);
  187.             [UIView commitAnimations];
  188.             yValueForSubviews = yValueForSubviews + viewBelowWhichItStarts.frame.size.height;
  189.         }
  190.     }
  191.     [scrollViewForAccordian setContentSize:CGSizeMake(320, yValueForSubviews)];
  192.     yValueForSubviews = viewBelowWhichItStarts.frame.origin.y;
  193.     [scrollViewForAccordian bringSubviewToFront:viewBelowWhichItStarts];
  194.     float yChek = yValueForSubviews;
  195.     // if needed to remove the previosly cretaed views
  196.     for (UIView * subviews in scrollViewForAccordian.subviews) {
  197.         if (subviews.tag == 0) {
  198.             if ((subviews.frame.origin.y - yChek) == 44) {
  199.                 [UIView beginAnimations:nil context:NULL];
  200.                 [UIView setAnimationDuration:0.3];
  201.                 subviews.frame = CGRectMake(0, yValueForSubviews, 320, viewBelowWhichItStarts.frame.size.height);
  202.                 [UIView commitAnimations];
  203.                 yChek +=44;
  204.                
  205.             }
  206.         }
  207.     }
  208. }
  209.  
  210.  
  211. @end
Advertisement
Add Comment
Please, Sign In to add comment