博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
UItableview里面的header、footer
阅读量:6330 次
发布时间:2019-06-22

本文共 5238 字,大约阅读时间需要 17 分钟。

#import "ViewController.h"#import "MJRefresh.h"@interface ViewController (){    UITableView *table;    NSArray * arr;    UIView * headerView;}@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];    arr =@[@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10",@"11",@"12",@"13",@"14",@"15",@"16",@"17",@"18",@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10",@"11",@"12",@"13",@"14",@"15",@"16",@"17",@"18"];    self.view.backgroundColor = [UIColor grayColor];    UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 200, 30)];    label.text =@"ffff";    label.textColor =[UIColor blackColor];    headerView =[[UIView alloc]initWithFrame:CGRectMake(0, 64, [UIScreen mainScreen].bounds.size.width,100)];    headerView.backgroundColor =[UIColor greenColor];    [headerView addSubview:label];    [self.view addSubview:headerView];            table = [[UITableView alloc]initWithFrame:CGRectMake(0, 174, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height) style:UITableViewStylePlain];    table.delegate = self;    table.dataSource = self;     [table.mj_header  beginRefreshing];    table.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(downRefresh)];    table.backgroundColor = [UIColor cyanColor];    [self.view addSubview:table];}-(void)downRefresh{    [table.mj_header endRefreshing];}-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{    return  arr.count;}-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{    return 2;}-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{            static NSString  *CellIdentifier = @"CellIdentifier";    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];     if (cell == nil) {     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];     }    cell.textLabel.text = arr [indexPath.row];    return  cell;}//-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{//    //    return @"为什么";//}//-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{        return 50;}-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{    return 50;}-(UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{        if (section==0) {    UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 50)];    view.backgroundColor = [UIColor redColor];    UILabel * footerlabel = [[UILabel alloc]initWithFrame:view.frame];    footerlabel.text = @"footer1";    footerlabel.textAlignment = NSTextAlignmentCenter;    [view addSubview:footerlabel];        return view;    }    else{        UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 50)];        UILabel * footerlabel = [[UILabel alloc]initWithFrame:view.frame];        footerlabel.text = @"footer2";        view.backgroundColor = [UIColor redColor];        footerlabel.textAlignment = NSTextAlignmentCenter;        [view addSubview:footerlabel];        return view;            }}-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{        if (section==0) {        UIView *Iview =[[UIView alloc]initWithFrame:CGRectMake(0, 0, 200, 50)];        Iview.backgroundColor =[UIColor orangeColor];        UILabel * footerlabel = [[UILabel alloc]initWithFrame:Iview.frame];        footerlabel.text = @"header1";        footerlabel.textAlignment = NSTextAlignmentCenter;        [Iview addSubview:footerlabel];        return Iview;    }    else{        UIView *Iview =[[UIView alloc]initWithFrame:CGRectMake(0, 0, 200, 50)];        Iview.backgroundColor =[UIColor cyanColor];        UILabel * footerlabel = [[UILabel alloc]initWithFrame:Iview.frame];        footerlabel.text = @"header2";        footerlabel.textAlignment = NSTextAlignmentCenter;        [Iview addSubview:footerlabel];        return Iview;            }    }-(void)scrollViewDidScroll:(UIScrollView *)scrollView{    if (scrollView.tag == 100102) {        UITableView *tableview = (UITableView *)scrollView;        CGFloat sectionHeaderHeight = 200;        CGFloat sectionFooterHeight = 10;        CGFloat offsetY = tableview.contentOffset.y;        if (offsetY >= 0 && offsetY <= sectionHeaderHeight)        {            tableview.contentInset = UIEdgeInsetsMake(-offsetY, 0, -sectionFooterHeight, 0);        }else if (offsetY >= sectionHeaderHeight && offsetY <= tableview.contentSize.height - tableview.frame.size.height - sectionFooterHeight)        {            tableview.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, -sectionFooterHeight, 0);        }else if (offsetY >= tableview.contentSize.height - tableview.frame.size.height - sectionFooterHeight && offsetY <= tableview.contentSize.height - tableview.frame.size.height)        {            tableview.contentInset = UIEdgeInsetsMake(-offsetY, 0, -(tableview.contentSize.height - tableview.frame.size.height - sectionFooterHeight), 0);        }    }}

 支持MJRefresh刷新可在header里面添加 滚动视图  footer里面添加按钮

转载于:https://www.cnblogs.com/sayimba/p/5698092.html

你可能感兴趣的文章