Skip to content

Instantly share code, notes, and snippets.

@andreamazz
Last active August 29, 2015 13:57
Show Gist options
  • Save andreamazz/9779843 to your computer and use it in GitHub Desktop.
Save andreamazz/9779843 to your computer and use it in GitHub Desktop.
AMScrollingNavbar with a UITableView created programmatically
//
// AMTableViewController.m
// ScrollingNavbarDemo
//
// Created by Andrea Mazzini on 09/11/13.
// Copyright (c) 2013 Andrea Mazzini. All rights reserved.
//
#import "AMTableViewController.h"
#import "UIViewController+ScrollingNavbar.h"
@interface AMTableViewController () <UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate>
@property (strong, nonatomic) UITableView *tableView;
@property (strong, nonatomic) NSArray* data;
@property (strong, nonatomic) UIView *topView;
@end
@implementation AMTableViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[self setTitle:@"Table View"];
self.data = @[@"Awesome content", @"Great content", @"Amazing content", @"Ludicrous content", @"Awesome content", @"Great content", @"Amazing content", @"Ludicrous content", @"Awesome content", @"Great content", @"Amazing content", @"Ludicrous content", @"Awesome content", @"Great content", @"Amazing content", @"Ludicrous content"];
self.edgesForExtendedLayout = UIRectEdgeNone;
self.tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
[self.view addSubview:self.tableView];
self.tableView.delegate = self;
self.tableView.dataSource = self;
[self followScrollView:self.tableView];
[self refreshNavbar];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[self showNavBarAnimated:NO];
}
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
// Call this after a small delay, or it won't work
[self performSelector:@selector(showNavbar) withObject:nil afterDelay:0.2];
}
- (BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView
{
// This enables the user to scroll down the navbar by tapping the status bar.
[self showNavbar];
return YES;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
return YES;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.data count];
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"Identifier"];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Identifier"];
}
cell.textLabel.text = self.data[indexPath.row];
return cell;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment