首先我们先集成一个UIView做一个自己的View

#import <UIKit/UIKit.h>

@interface MyView : UIView

@property (nonatomic,strong)UIView * commentView;

@end

在实现文件里重写系统的- (UIView*)hitTest:(CGPoint)point withEvent:(UIEvent *)event方法

#import "MyView.h"

@implementation MyView

- (UIView*)hitTest:(CGPoint)point withEvent:(UIEvent *)event

{

UIView *result = [super hitTest:point withEvent:event];

if (result == self) {

return nil;

} else {

return result;

}

}

@end

然后我们转回到ViewController里首先我们导入我们实现的MyView,并创建一个它的属性,然后写一个UITableView的属性,并实现它的dai代理,

#import "ViewController.h"

#import "MyView.h"

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,strong)UITableView * tableView;

@property (nonatomic,strong)MyView * headerView;

@end

接下来 我们就可以实现TableVeiw的代理方法和创建,然后我们设置TableView和headerView并用观察者观察tableVeiw的偏移量,代码实现如下

- (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

self.automaticallyAdjustsScrollViewInsets = NO;

self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height) style:UITableViewStylePlain];

self.tableView.dataSource = self;

self.tableView.delegate = self;

[self.view addSubview:self.tableView];

[self.tableView addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew context:nil];

self.headerView = [[MyView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200 )];

self.moreButtonView.frame = CGRectMake(0, self.headerView.frame.size.height - 50, [UIScreen mainScreen].bounds.size.width, 50) ;

self.headerView.backgroundColor =[UIColor redColor];

[self.headerView addSubview:self.moreButtonView];

[self.view addSubview:self.headerView];

}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context

{

if ([keyPath isEqualToString:@"contentOffset"])

{

CGPoint offset = [change[NSKeyValueChangeNewKey] CGPointValue];

NSLog(@"%f",offset.y);

if (offset.y <= 0 && -offset.y >= 114) {

if (-offset.y >= 200) {

return;

}

//            UILabel * label = (UILabel*)[self.headerView viewWithTag:110];

//            [label removeFromSuperview];

CGRect newFrame = CGRectMake(0, 0, self.view.frame.size.width, -offset.y);

[UIView animateWithDuration:1 animations:^{

[self.myLabel removeFromSuperview];

self.headerView.backgroundColor = [UIColor redColor];

}];

self.headerView.frame = newFrame;

CGRect newViewRect = CGRectMake(0, self.headerView.bounds.size.height - 64 , [UIScreen mainScreen].bounds.size.width, 64);

UIView * view = (UIView*)[self.headerView viewWithTag:110];

view.frame = newViewRect;

if (-offset.y <=200)

{

self.tableView.contentInset = UIEdgeInsetsMake(-offset.y, 0, 0, 0);

}

self.moreButtonView.frame = CGRectMake(0, self.headerView.frame.size.height - 50, [UIScreen mainScreen].bounds.size.width, 50) ;

} else {

CGRect newFrame = CGRectMake(0, 0, self.view.frame.size.width, 64);

self.headerView.frame = newFrame;

//            CGRect viewRect = CGRectMake(0, self.headerView.bounds.size.height - 64 , [UIScreen mainScreen].bounds.size.width, 64);

//            UIView * view = (UIView*)[self.headerView viewWithTag:110];

//            view.frame = viewRect;

[UIView animateWithDuration:1.0 animations:^{

[self.headerView addSubview:self.myLabel];

self.headerView.backgroundColor= [UIColor lightGrayColor];

}];

self.tableView.contentInset = UIEdgeInsetsMake(64, 0, 0, 0);

self.moreButtonView.frame = CGRectMake(0, self.headerView.frame.size.height - 50, [UIScreen mainScreen].bounds.size.width, 50) ;

}

}

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

return 1;

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

return 30;

}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];

if (!cell) {

cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];

}

if (indexPath.row == 0) {

cell.textLabel.text =@"123";

}else

cell.textLabel.text = @"哈哈哈";

return cell;

}

//- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

//{

//    UIView * view  = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 100)];

//    view.backgroundColor = [UIColor greenColor];

//    return view;

//}

//- (NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section

//{

//    return @"1";

//}

//- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

//{

//    SKStoreProductViewController * vc = [[SKStoreProductViewController alloc] init];

//    vc.delegate = self;

//    [vc loadProductWithParameters:

//     //appId唯一的

//  @{SKStoreProductParameterITunesItemIdentifier : @"587767923"} completionBlock:^(BOOL result, NSError *error) {

//      //block回调

//      if(error){

//          NSLog(@"error %@ with userInfo %@",error,[error userInfo]);

//      }else{

//          //模态弹出appstore

//          [self presentViewController:vc animated:YES completion:^{

//

//          }

//           ];

//      }

//  }];

//

//}

//- (UIView *)moreButtonView

//{

//    if (_moreButtonView == nil) {

//

//        _moreButtonView = [[UIView alloc] init];

//        _moreButtonView.backgroundColor = [UIColor yellowColor];

//

//        NSArray * arr = @[@"动态",@"关注",@"粉丝"];

//

//        for (int i= 0; i<3; i++) {

//            UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];

//            button.frame = CGRectMake(([UIScreen mainScreen].bounds.size.width/3)*i, 0, [UIScreen mainScreen].bounds.size.width/3, 50);

//            [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

//            [button setTitle:[arr objectAtIndex:i] forState:UIControlStateNormal];

//            button.tag = 100 + i;

//            [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];

//            [_moreButtonView addSubview:button];

//        }

//

//

//

//    }

//    return _moreButtonView;

//}

- (NSString*)sql

{

return  @"jianbo_su";

}

- (void)buttonClick:(UIButton*)btn

{

if (btn.tag == 100) {

NSLog(@"su");

UIView * mainView = [[UIView alloc] initWithFrame:self.view.bounds];

mainView.tag = 250;

mainView.backgroundColor = [UIColor lightGrayColor];

mainView.alpha = 0.9;

[self.view addSubview:mainView];

UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 150, 150)];

view.backgroundColor = [UIColor whiteColor];

view.center = self.view.center;

UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];

[view addGestureRecognizer:tap];

[mainView addSubview:view];

}

if (btn.tag == 101) {

NSLog(@"jian");

}

if (btn.tag == 102) {

NSLog(@"bo");

}

}

- (void)tapAction:(UITapGestureRecognizer*)tap

{

UIView * view = (UIView*)[self.view viewWithTag:250];

[view removeFromSuperview];

}

- (UILabel *)myLabel

{

if (_myLabel == nil) {

_myLabel = [[UILabel alloc] initWithFrame:CGRectMake(([UIScreen mainScreen].bounds.size.width/2) - 75 , 10, 150, 40)];

_myLabel.text = @"苏建波";

_myLabel.textAlignment = NSTextAlignmentCenter;

_myLabel.tag = 110;

_myLabel.textColor = [UIColor blackColor];

[self.headerView addSubview:_myLabel];

}

return _myLabel;

}

接下来我们可以看一下效果

需要demo的联系qq:1956305923

UITableView控制头部View的更多相关文章

  1. 简单的拖动手势控制侧拉view显示

    通过 UIPanGestureRecognizer  手势来控制侧拉view的显示 在QHLViewController.m文件中,先添加一些宏定义和参数等等. #define QHLAnimatin ...

  2. UIScrollView上面放一个UIScrollView或者UITableView拖动时候 View出现一闪一闪解决办法

    在项目中发现一个问题: 创建一个UIScrollView 上面放一个scrollView或者TableView,拖动scrollview或TableView 画面出现一闪一闪的情况. 解决办法设置一下 ...

  3. android 关于 webview 控制其它view的显示 以及更改view数据失败的问题总结

    总结: 1.webview 无法直接更改view的属性,可以通过 handler实现,因为跨线程 2.webview可以通过js api读取 view的属性值 代码: private Handler ...

  4. oc 把view添加到rootcontrollerview控制的view

    在当前活跃的window 添加一个view [[[[[UIApplication sharedApplication] keyWindow] rootViewController] view] add ...

  5. iOS应用架构谈 view层的组织和调用方案

    当我们开始设计View层的架构时,往往是这个App还没有开始开发,或者这个App已经发过几个版本了,然后此时需要做非常彻底的重构. 一般也就是这两种时机会去做View层架构,基于这个时机的特殊性,我们 ...

  6. (转)iOS应用架构谈 view层的组织和调用方案

    前言 <iOS应用架构谈 开篇>出来之后,很多人来催我赶紧出第二篇.这一篇文章出得相当艰难,因为公司里的破事儿特别多,我自己又有点私事儿,以至于能用来写博客的时间不够充分. 现在好啦,第二 ...

  7. iOS开发——高级UI&带你玩转UITableView

    带你玩装UITableView 在实际iOS开发中UITableView是使用最多,也是最重要的一个控件,如果你不会用它,那别说什么大神了,菜鸟都不如. 其实关于UItableView事非常简单的,实 ...

  8. UITableView实现分组, 并且点击每个分组后展开

    效果图: 简单说下实现思路: 数据传过来之后, 先创建好对应个数的分组头部View, 也就是要在 - (UIView *)tableView:(UITableView *)tableView view ...

  9. Swift - 使用表格组件(UITableView)实现分组列表

    1,样例说明: (1)列表以分组的形式展示 (2)同时还自定义分区的头部和尾部 (3)点击列表项会弹出消息框显示该项信息. 2,效果图:       3,代码如下: 1 2 3 4 5 6 7 8 9 ...

随机推荐

  1. C#Redis字符串

    上周六通宵打牌周日白天只睡3小时累成狗,从今天起以后不能玩太大的了,小赌怡情大赌伤身,和同事朋友有空玩玩还是好的.今天公司外面马路上有人挂灯笼时死了一个人,哎,快过年了悲剧又发生了,真是生命是脆弱的. ...

  2. arcengine 开发经典帖

    http://bbs.esrichina-bj.cn/ESRI/viewthread.php?tid=25575&page=1&extra= 使用ArcGIS Engine 开发自定义 ...

  3. python命令调用函数os.popen

    参考自xerosploit 描述:利用os.popen()函数调用系统命令nmap进行扫描,并用grep命令对扫描结果关键内容进行提取 代码 #!/usr/bin/env pthon #--*--co ...

  4. Sipdroid实现SIP(六): SIP中的请求超时和重传

    目录 一. Sipdroid的请求超时和重传 二. SIP中超时和重传的定义 三. RFC中超时和重传的定义 一. Sipdroid的请求超时和重传 Sipdroid实现SIP协议栈系列, 之前的文章 ...

  5. c# PictureBox 的图像上使用鼠标画矩形框

    C# 中在图像上画框,通过鼠标来实现主要有四个消息响应函数MouseDown, MouseMove, MouseUp, Paint重绘函数实现.当鼠标键按下时开始画框,鼠标键抬起时画框结束. Poin ...

  6. Windows下载地址

    文件名 cn_windows_7_professional_with_sp1_x64_dvd_u_677031.iso SHA1 9B57E67888434C24DD683968A3CE2C72755 ...

  7. 极路由器刷机安装ss插件最新教程

    极路由器系统升级后,旧的插件已不可用,这里是最新极路由器刷机教程,可实现绑定ss代理账号的功能. 获取root权限 安装开发者插件,获取root权限,请先登录极路由器后台(电脑浏览器访问 192.16 ...

  8. IOS 成员变量,全局变量,局部变量定义,static与extern的区别

    IOS 成员变量,全局变量,局部变量定义,static与extern的区别 1,先说定义 1)成员变量定义:生存与该类的生命周期,变量存活周期跟你定义的该类实体对象一样:作用域是整个实体对象:可以在h ...

  9. 重要的几个热键[Tab], [ctrl]-c, [ctrl]-d

    来源于:鸟哥的Linux私房菜 在继续后面的章节之前,这里很需要跟大家再来报告一件事,那就是我们的文字模式里头具有很多的功能按键, 这些按键可以辅助我们进行指令的编写与程序的中断呢!这几个按键请大家务 ...

  10. 转:Spark User Defined Aggregate Function (UDAF) using Java

    Sometimes the aggregate functions provided by Spark are not adequate, so Spark has a provision of ac ...