Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // ViewController.m
- // TEST
- //
- // Created by Karol Kurek on 02.11.2017.
- // Copyright © 2017 Karol Kurek. All rights reserved.
- //
- #import "ViewController.h"
- @interface ViewController () <UITableViewDelegate, UITableViewDataSource, UISearchResultsUpdating, UISearchControllerDelegate, UISearchBarDelegate>
- @property (nonatomic, strong) NSMutableArray<NSArray<NSString *> *> *someData;
- @property (nonatomic, strong) NSMutableArray<NSArray<NSString *> *> *filteredData;
- @property (nonatomic, strong) UITableView *tableView;
- @end
- @implementation ViewController
- - (instancetype)initWithCoder:(NSCoder *)aDecoder {
- self = [super initWithCoder:aDecoder];
- if (self) {
- self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
- [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
- [self.tableView setBackgroundColor:[UIColor clearColor]];
- self.someData = [NSMutableArray array];
- [self.someData addObject:[NSArray arrayWithObjects:@"a",@"b",@"c", nil]];
- [self.someData addObject:[NSArray arrayWithObjects:@"a",@"b",@"c", nil]];
- [self.someData addObject:[NSArray arrayWithObjects:@"a",@"b",@"c", nil]];
- [self.someData addObject:[NSArray arrayWithObjects:@"a",@"b",@"c", nil]];
- self.tableView.showsVerticalScrollIndicator = NO;
- self.filteredData = [NSMutableArray array];
- }
- return self;
- }
- - (void)loadView {
- [super loadView];
- [self.tableView setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
- self.tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
- self.tableView.delegate = self;
- self.tableView.dataSource = self;
- self.tableView.translatesAutoresizingMaskIntoConstraints = NO;
- self.tableView.estimatedRowHeight = 85.0;
- self.tableView.rowHeight = UITableViewAutomaticDimension;
- [self.view addSubview:self.tableView];
- }
- - (void)viewDidLoad {
- [super viewDidLoad];
- UISearchController * search = [[UISearchController alloc] initWithSearchResultsController:nil];
- search.searchResultsUpdater = self;
- self.navigationItem.searchController = search;
- self.navigationItem.hidesSearchBarWhenScrolling = YES;
- self.navigationItem.searchController.searchBar.barTintColor = [UIColor blueColor];
- self.navigationItem.searchController.searchBar.delegate = self;
- UITextField *textField = [self.navigationItem.searchController.searchBar valueForKey:@"searchField"];
- UIView *backgroundView = textField.subviews.firstObject;
- backgroundView.backgroundColor = [UIColor whiteColor];
- backgroundView.layer.cornerRadius = 10;
- backgroundView.clipsToBounds = YES;
- }
- - (NSMutableArray<NSArray<NSString *> *> *)currentTableData {
- if([self.filteredData count] && [self.filteredData[0] count]){
- return self.filteredData;
- }else{
- return self.someData;
- }
- return nil;
- }
- -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
- return [[self currentTableData] count];
- }
- -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
- return [[self currentTableData][section] count];
- }
- -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
- UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"reuse"];
- [[cell textLabel] setText:[self currentTableData][indexPath.section][indexPath.row]];
- [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
- return cell;
- }
- -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
- return [UIView new];
- }
- -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
- return CGFLOAT_MIN;
- }
- -(CGFloat)tableView:(UITableView*)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath*)indexPath{
- return [self getHeightForCellInTableView:tableView andIndexPath:indexPath];
- }
- -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
- return [self getHeightForCellInTableView:tableView andIndexPath:indexPath];
- }
- -(CGFloat)getHeightForCellInTableView:(UITableView *)tableView andIndexPath:(NSIndexPath *)indexPath{
- return 40;
- }
- -(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
- return [UIView new];
- }
- -(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
- return 0.1f;
- }
- - (void)updateSearchResultsForSearchController:(UISearchController *)searchController {
- self.filteredData = [self filterContentForSearchText:searchController.searchBar.text];
- [self.tableView reloadData];
- }
- - (NSMutableArray<NSArray<NSString *> *> *)filterContentForSearchText:(NSString *)text {
- NSMutableArray<NSArray<NSString *> *> *sections = [NSMutableArray array];
- for (NSArray *section in self.currentTableData) {
- NSArray *objects = [section filteredArrayUsingPredicate:[self predicateForSearching:text]];
- if ([objects count] > 0) {
- [sections addObject:[NSArray arrayWithArray:objects]];
- }
- }
- return sections;
- }
- - (NSPredicate *)predicateForSearching:(NSString *)text {
- return [NSPredicate predicateWithBlock:^BOOL(id _Nullable evaluatedObject, NSDictionary<NSString *,id> * _Nullable bindings) {
- if ([evaluatedObject isKindOfClass:[NSString class]]) {
- return [((NSString *)evaluatedObject) containsString:text];
- }
- return NO;
- }];
- }
- @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement