vc.h
#import <UIKit/UIKit.h>
@interface StickyViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UILabel *v2l;
@property (weak, nonatomic) IBOutlet UITableView *myTableView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *height_contraint;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *content_height;
@property (weak, nonatomic) IBOutlet UIView *view1;
@property (weak, nonatomic) IBOutlet UIView *v2;
@end
vc.m
#import "StickyViewController.h"
#import "StickyTableViewCell.h"
@interface StickyViewController (){
NSDictionary *animals;
NSArray *animalSectionTitles;
}
@property (assign, nonatomic) BOOL isFinding;
@property (assign, nonatomic) CGFloat previousOffset;
@end
@implementation StickyViewController
- (void)viewDidLoad {
[super viewDidLoad];
_myTableView.delegate=self;
_myTableView.dataSource=self;
animals = @{@"B" : @[@"Bear", @"Black Swan", @"Buffalo"],
@"C" : @[@"Camel", @"Cockatoo"],
@"D" : @[@"Dog", @"Donkey"],
@"E" : @[@"Emu"],
@"G" : @[@"Giraffe", @"Greater Rhea"],
@"H" : @[@"Hippopotamus", @"Horse"],
@"K" : @[@"Koala"],
@"L" : @[@"Lion", @"Llama"],
@"M" : @[@"Manatus", @"Meerkat"],
@"P" : @[@"Panda", @"Peacock", @"Pig", @"Platypus", @"Polar Bear"],
@"R" : @[@"Rhinoceros"],
@"S" : @[@"Seagull"],
@"T" : @[@"Tasmania Devil"],
@"W" : @[@"Whale", @"Whale Shark", @"Wombat"]};
animalSectionTitles = [[animals allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return [animalSectionTitles count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [animalSectionTitles objectAtIndex:section];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
NSString *sectionTitle = [animalSectionTitles objectAtIndex:section];
NSArray *sectionAnimals = [animals objectForKey:sectionTitle];
return [sectionAnimals count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
// Configure the cell...
NSString *sectionTitle = [animalSectionTitles objectAtIndex:indexPath.section];
NSArray *sectionAnimals = [animals objectForKey:sectionTitle];
NSString *animal = [sectionAnimals objectAtIndex:indexPath.row];
cell.textLabel.text = animal;
// Configure the cell...
return cell;
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
self.isFinding = YES;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
if (self.isFinding) {
if (self.previousOffset == 0) {
self.previousOffset = self.myTableView.contentOffset.y;
} else {
CGFloat diff = self.myTableView.contentOffset.y - self.previousOffset;
if (diff != 0) {
self.previousOffset = 0;
self.isFinding = NO;
if (diff > 0) {
// moved up
// NSLog(@"top");
_v2.hidden=YES;
_height_contraint.constant=0;
} else {
NSLog(@"%f",_myTableView.contentOffset.y);
if (_myTableView.contentOffset.y < 120)
{ // TOP
_v2.hidden=NO;
_height_contraint.constant=100;
}
// moved down
// NSLog(@"down");
//_v2.hidden=NO;
// _height_contraint.constant=100;
}
}
}
}
}
@end
Sign up here with your email
ConversionConversion EmoticonEmoticon