#import "ViewController.h"
//簽署協(xié)議
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
// 添加 tableView
@property (nonatomic ,strong) UITableView *dragTableview;
// imageView
@property (nonatomic ,strong) UIImageView *photoImageview;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//讀取plist文件當中的數(shù)據(jù)
NSString *bb = [[NSBundle mainBundle] pathForResource:@"22" ofType:@"plist"];
NSDictionary *dd= [[NSDictionary alloc] initWithContentsOfFile:bb];
// 一步到位
[self.view addSubview:self.dragTableview];
// 先創(chuàng)建一個頭部視圖
UIView * headerview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, 200)];
// 設置顏色
//? ? headerview.backgroundColor = [UIColor colorWithRed:245/255.0 green:120/255.0 blue:111/255.0 alpha:0.2];
// 直接加載到內(nèi)存中去了
self.photoImageview.image = [UIImage imageNamed:@"headerImage1.jpg"];
//將圖片添加到頭視圖
[headerview addSubview:self.photoImageview];
// 設置頭部視圖
self.dragTableview.tableHeaderView = headerview;
UIView * backgroundView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight)];
[backgroundView addSubview:self.photoImageview];
self.dragTableview.backgroundView = backgroundView;
}
// 拖動的時候,調(diào)用這個方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
NSLog(@"%f",scrollView.contentOffset.y);
// 先取出來 imageView 的 frame
CGRect tempRect = self.photoImageview.frame;
if (scrollView.contentOffset.y > 0) {
// 向上滾動
tempRect.origin.y = -scrollView.contentOffset.y;
// 賦值回來
self.photoImageview.frame = tempRect;
}else {
// 向下 滾動? 圖片放大(在原來的高度基礎上放大) 肯定是跟 contentOffSet 有關系
tempRect.origin.y = 0;
tempRect.size.height = 200 - scrollView.contentOffset.y;
// 把修改后的 frame? 賦值回去
self.photoImageview.frame = tempRect;
}
}
#pragma mark --返回組數(shù) return? sections
// 返回組數(shù)
//- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
//
//? ? return nil;
//}
// 返回行數(shù)
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 15;
}
// 返回 cell
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString * cellID = @"cellID";
//
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if (!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
// 設置顏色
cell.contentView.backgroundColor = [self randomColor];
}
cell.textLabel.text = [NSString stringWithFormat:@"第%ld個",indexPath.row];
return cell;
}
// 懶加載 lazyloading
- (UITableView *)dragTableview {
// 不會重復加載
if (!_dragTableview) {
_dragTableview? = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight) style:UITableViewStylePlain];
_dragTableview.delegate = self;
_dragTableview.dataSource = self;
}
return _dragTableview;
}
- (UIImageView *)photoImageview {
if (!_photoImageview) {
_photoImageview = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, 200)];
// 設置填充方式
_photoImageview.contentMode = UIViewContentModeScaleAspectFill;
}
return _photoImageview;
}
//隨機顏色
- (UIColor *)randomColor
{
CGFloat r = arc4random() % 256 / 255.0;
CGFloat g = arc4random() % 256 / 255.0;
CGFloat b = arc4random() % 256 / 255.0;
return [UIColor colorWithRed:r green:g blue:b alpha:0.7];
}
@end