Advertisement
Guest User

Untitled

a guest
Jan 16th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. //  ViewController.m
  3. //  Lesson3
  4. //
  5. //  Created by Alexey  Levanov on 11.01.17.
  6. //  Copyright © 2017 AlexeyLevanov. All rights reserved.
  7. //
  8.  
  9. #import "ViewController.h"
  10. #import "PhoneBookCell.h"
  11.  
  12.  
  13. #define VIEW_HEIGHT 100
  14. #define TAG 1000
  15.  
  16. @interface ViewController () <UITableViewDelegate, UITableViewDataSource>
  17.  
  18. @end
  19.  
  20. @implementation ViewController
  21. {
  22.     UIButton *button;
  23.     UILabel *label;
  24.    
  25.     UITableView *testTableView;
  26. }
  27.  
  28. - (void)viewDidLoad
  29. {
  30.     [super viewDidLoad];
  31.    
  32.     [self setupTableView];
  33. }
  34.  
  35.  
  36. - (void)didReceiveMemoryWarning
  37. {
  38.     [super didReceiveMemoryWarning];
  39.    
  40. }
  41.  
  42.  
  43. #pragma mark - UI
  44.  
  45. - (void)createUI
  46. {
  47.     self.view.backgroundColor = [UIColor greenColor];
  48.    
  49.     label = [[UILabel alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - 50, self.view.frame.size.width, 50)];
  50.     label.textAlignment = NSTextAlignmentCenter;
  51.     label.text = @"Нажми на кнопку";
  52.     label.textColor = [UIColor whiteColor];
  53.     [self.view addSubview:label];
  54.    
  55.     button = [UIButton buttonWithType:UIButtonTypeCustom];
  56.     button.frame = CGRectMake(0, 0, self.view.frame.size.width, 40);
  57.     button.backgroundColor = [UIColor colorWithRed:213/255.0 green:112/255.0 blue:207/255.0 alpha:1];
  58.     [button setTitle:@"Кнопка" forState:UIControlStateNormal];
  59.     [button addTarget:self action:@selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];
  60.     [self.view addSubview:button];
  61. }
  62.  
  63. - (void)setupTableView
  64. {
  65.     testTableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
  66.     testTableView.delegate = self;
  67.     testTableView.dataSource = self;
  68.     [self.view addSubview:testTableView];
  69. }
  70.  
  71.  
  72. #pragma mark - actions
  73.  
  74. - (void)buttonPressed
  75. {
  76.     label.center = CGPointMake(self.view.center.x, 500);
  77.     label.backgroundColor = [UIColor redColor];
  78.     label.text = @"Кнопка нажата";
  79.     UIView *tempView;
  80.     for (int i=0; i<self.view.subviews.count; i++)
  81.     {
  82.         UIView *view = self.view.subviews[i];
  83.         if (view.tag == TAG)
  84.             view.backgroundColor = [UIColor colorWithRed:arc4random()%255/255.0 green:arc4random()%255/255.0 blue:arc4random()%255/255.0 alpha:1];
  85.     }
  86.     if (!tempView)
  87.     {
  88.         tempView = [[UIView alloc] initWithFrame:CGRectMake(self.view.center.x - VIEW_HEIGHT/2, self.view.center.y - VIEW_HEIGHT/2, VIEW_HEIGHT, VIEW_HEIGHT)];
  89.         tempView.tag = TAG;
  90.         tempView.backgroundColor = [UIColor colorWithRed:arc4random()%255/255.0 green:arc4random()%255/255.0 blue:arc4random()%255/255.0 alpha:1];
  91.         [self.view addSubview:tempView];
  92.     }
  93.  
  94. }
  95.  
  96.  
  97. #pragma mark - UITableView Delegate/DataSource
  98.  
  99. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  100. {
  101.     return 10;
  102. }
  103.  
  104. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  105. {
  106.     PhoneBookCell *tableViewCell = [tableView dequeueReusableCellWithIdentifier:@"testCell"];
  107.     if (!tableViewCell)
  108.         tableViewCell = [[PhoneBookCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"testCell"];
  109.        
  110.     tableViewCell.textLabel.text = @"Номер строки";
  111.     [tableView setNeedsLayout];
  112.     return tableViewCell;
  113. }
  114.  
  115. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  116. {
  117.    
  118. }
  119.  
  120.  
  121. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  122. {
  123.     return 80.0f;
  124. }
  125.  
  126.  
  127. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement