Advertisement
Guest User

Example

a guest
Nov 2nd, 2017
381
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. //  ViewController.m
  3. //  TEST
  4. //
  5. //  Created by Karol Kurek on 02.11.2017.
  6. //  Copyright © 2017 Karol Kurek. All rights reserved.
  7. //
  8.  
  9. #import "ViewController.h"
  10.  
  11. @interface ViewController () <UITableViewDelegate, UITableViewDataSource, UISearchResultsUpdating, UISearchControllerDelegate, UISearchBarDelegate>
  12. @property (nonatomic, strong) NSMutableArray<NSArray<NSString *> *> *someData;
  13. @property (nonatomic, strong) NSMutableArray<NSArray<NSString *> *> *filteredData;
  14. @property (nonatomic, strong) UITableView *tableView;
  15. @end
  16.  
  17. @implementation ViewController
  18.  
  19. - (instancetype)initWithCoder:(NSCoder *)aDecoder {
  20.     self = [super initWithCoder:aDecoder];
  21.     if (self) {
  22.         self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
  23.         [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
  24.         [self.tableView setBackgroundColor:[UIColor clearColor]];
  25.         self.someData = [NSMutableArray array];
  26.         [self.someData addObject:[NSArray arrayWithObjects:@"a",@"b",@"c", nil]];
  27.         [self.someData addObject:[NSArray arrayWithObjects:@"a",@"b",@"c", nil]];
  28.         [self.someData addObject:[NSArray arrayWithObjects:@"a",@"b",@"c", nil]];
  29.         [self.someData addObject:[NSArray arrayWithObjects:@"a",@"b",@"c", nil]];
  30.         self.tableView.showsVerticalScrollIndicator = NO;
  31.         self.filteredData = [NSMutableArray array];
  32.     }
  33.     return self;
  34. }
  35.  
  36. - (void)loadView {
  37.     [super loadView];
  38.     [self.tableView setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
  39.     self.tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
  40.     self.tableView.delegate = self;
  41.     self.tableView.dataSource = self;
  42.     self.tableView.translatesAutoresizingMaskIntoConstraints = NO;
  43.    
  44.     self.tableView.estimatedRowHeight = 85.0;
  45.     self.tableView.rowHeight = UITableViewAutomaticDimension;
  46.    
  47.     [self.view addSubview:self.tableView];
  48. }
  49.  
  50. - (void)viewDidLoad {
  51.     [super viewDidLoad];
  52.    
  53.     UISearchController * search = [[UISearchController alloc] initWithSearchResultsController:nil];
  54.     search.searchResultsUpdater = self;
  55.    
  56.     self.navigationItem.searchController = search;
  57.     self.navigationItem.hidesSearchBarWhenScrolling = YES;
  58.     self.navigationItem.searchController.searchBar.barTintColor = [UIColor blueColor];
  59.     self.navigationItem.searchController.searchBar.delegate = self;
  60.    
  61.     UITextField *textField = [self.navigationItem.searchController.searchBar valueForKey:@"searchField"];
  62.     UIView *backgroundView = textField.subviews.firstObject;
  63.     backgroundView.backgroundColor = [UIColor whiteColor];
  64.     backgroundView.layer.cornerRadius = 10;
  65.     backgroundView.clipsToBounds = YES;
  66.    
  67. }
  68.  
  69. - (NSMutableArray<NSArray<NSString *> *> *)currentTableData {
  70.     if([self.filteredData count] && [self.filteredData[0] count]){
  71.         return self.filteredData;
  72.     }else{
  73.         return self.someData;
  74.     }
  75.     return nil;
  76. }
  77.  
  78. -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  79.     return [[self currentTableData] count];
  80. }
  81.  
  82. -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  83.     return [[self currentTableData][section] count];
  84. }
  85.  
  86. -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  87.     UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"reuse"];
  88.     [[cell textLabel] setText:[self currentTableData][indexPath.section][indexPath.row]];
  89.    
  90.     [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
  91.    
  92.     return cell;
  93. }
  94.  
  95. -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
  96.     return [UIView new];
  97.    
  98. }
  99.  
  100. -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
  101.     return CGFLOAT_MIN;
  102. }
  103.  
  104. -(CGFloat)tableView:(UITableView*)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath*)indexPath{
  105.     return [self getHeightForCellInTableView:tableView andIndexPath:indexPath];
  106. }
  107.  
  108. -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
  109.     return [self getHeightForCellInTableView:tableView andIndexPath:indexPath];
  110. }
  111.  
  112. -(CGFloat)getHeightForCellInTableView:(UITableView *)tableView andIndexPath:(NSIndexPath *)indexPath{
  113.     return 40;
  114. }
  115.  
  116. -(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
  117.     return [UIView new];
  118. }
  119. -(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
  120.     return 0.1f;
  121. }
  122.  
  123. - (void)updateSearchResultsForSearchController:(UISearchController *)searchController {
  124.     self.filteredData = [self filterContentForSearchText:searchController.searchBar.text];
  125.    
  126.     [self.tableView reloadData];
  127. }
  128.  
  129. - (NSMutableArray<NSArray<NSString *> *> *)filterContentForSearchText:(NSString *)text {
  130.     NSMutableArray<NSArray<NSString *> *> *sections = [NSMutableArray array];
  131.    
  132.     for (NSArray *section in self.currentTableData) {
  133.         NSArray *objects = [section filteredArrayUsingPredicate:[self predicateForSearching:text]];
  134.        
  135.         if ([objects count] > 0) {
  136.             [sections addObject:[NSArray arrayWithArray:objects]];
  137.         }
  138.     }
  139.    
  140.     return sections;
  141. }
  142.  
  143. - (NSPredicate *)predicateForSearching:(NSString *)text {
  144.     return [NSPredicate predicateWithBlock:^BOOL(id  _Nullable evaluatedObject, NSDictionary<NSString *,id> * _Nullable bindings) {
  145.         if ([evaluatedObject isKindOfClass:[NSString class]]) {
  146.             return [((NSString *)evaluatedObject) containsString:text];
  147.         }
  148.        
  149.         return NO;
  150.     }];
  151. }
  152.  
  153. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement