详细项目链接:Github地址:https://github.com/MinLee6/WeiboUserHomePage
创建控制器LMMyHomeController.h
//
// LMMyHomeController.h
// lingfo
//
// Created by limin on 16/5/24.
// Copyright © 2016年 anxin. All rights reserved.
//#import <UIKit/UIKit.h>
@interface LMMyHomeController : UIViewController@end
创建控制器LMMyHomeController.m
//
// LMMyHomeController.m
// lingfo
//
// Created by limin on 16/5/24.
// Copyright © 2016年 anxin. All rights reserved.
//#import "LMMyHomeController.h"
#import "HHHorizontalPagingView.h"
#import "UIView+WhenTappedBlocks.h"
#import "HomePageContentTableVC.h"#import "LMMyHomeController.h"#define kHeaderTopHeight 200@interface ArtNavView : UIView@property (nonatomic, strong) UIButton *leftBut;
/* 标题 */
@property(nonatomic,strong)UILabel *navTitleLabel;@end@implementation ArtNavView- (instancetype)initWithFrame:(CGRect)frame {if (self = [super initWithFrame:frame]) {[self configUI];}return self;
}- (void) configUI {/** 只是做一个简单示例,要加分割线或其它变化,自行扩展即可*/self.backgroundColor = [UIColor colorWithRed:255/255.0 green:214/255.0 blue:116/255.0 alpha:0];;UIButton *but = [UIButton buttonWithType:UIButtonTypeSystem];but.frame = CGRectMake(0, 22, 44, 44);UIImage *buttonimage = [UIImage imageNamed:@"barbuttonicon_back"];[but setImage:buttonimage forState:UIControlStateNormal];but.tintColor = [UIColor colorWithWhite:1 alpha: 1];self.leftBut = but;[self addSubview:but];UILabel *navTitleLabel = [[UILabel alloc]initWithFrame:CGRectMake((kScreenWidth-100)*0.5, 22, 100, 44)];navTitleLabel.textColor = [UIColor colorWithWhite:1 alpha: 0];if ([UIDevice currentDevice].systemVersion.floatValue <= 8.0) {[navTitleLabel setFont:[UIFont systemFontOfSize:21 weight:0.1]];}else{[navTitleLabel setFont:[UIFont systemFontOfSize:21 weight:0.1]];}navTitleLabel.textAlignment = NSTextAlignmentCenter;self.navTitleLabel = navTitleLabel;[self addSubview:navTitleLabel];}- (void)changeAlpha:(CGFloat)alpha {self.backgroundColor = [UIColor colorWithRed:214/255.0 green:116/255.0 blue:0/255.0 alpha:alpha];self.navTitleLabel.textColor = [UIColor colorWithWhite:1 alpha: alpha];
// self.leftBut.tintColor = [UIColor colorWithWhite:(1 - alpha) alpha:1];
}@end@interface LMMyHomeController ()<HHHorizontalPagingViewDelegate>@property (nonatomic, strong) HHHorizontalPagingView *pagingView;@property (nonatomic, strong) ArtNavView *navView;
/* 按钮 */
@property(nonatomic,strong)NSMutableArray *buttonArray;
/* 多个数组 */
@property(nonatomic,strong)NSMutableArray *contentVCArrays;
// tableHeaderView
@property (nonatomic, strong) UIView *headerView;@end@implementation LMMyHomeController
#pragma mark - GET方法
- (UIView *)headerView
{if(_headerView == nil){_headerView = [[UIView alloc]init];_headerView.backgroundColor = [UIColor brownColor];_headerView.frame = CGRectMake(0, 0, kScreenWidth, kHeaderTopHeight);//添加头像UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"L7"]];imageView.frame = CGRectMake((kScreenWidth-80)*0.5, (kHeaderTopHeight-80)*0.5, 80, 80);imageView.clipsToBounds = YES;imageView.layer.cornerRadius = 40;[_headerView addSubview:imageView];}return _headerView;
}-(NSMutableArray *)contentVCArrays
{if (!_contentVCArrays) {_contentVCArrays = [NSMutableArray array];}return _contentVCArrays;
}
#pragma mark - 懒加载
- (HHHorizontalPagingView *)pagingView {if (!_pagingView) {CGSize size = [UIScreen mainScreen].bounds.size;_pagingView = [[HHHorizontalPagingView alloc] initWithFrame:CGRectMake(0, 0, size.width, size.height) delegate:self];_pagingView.segmentTopSpace = kNavBarHeight;_pagingView.segmentView.backgroundColor = kBgColor;_pagingView.maxCacheCout = 5.;_pagingView.isGesturesSimulate = YES;[self.view addSubview:_pagingView];}return _pagingView;
}
#pragma mark - View配置
- (void)viewDidAppear:(BOOL)animated {[super viewDidAppear:animated];
}- (void)viewWillAppear:(BOOL)animated {[self.navigationController setNavigationBarHidden:YES animated:animated];[super viewWillAppear:animated];self.navView.navTitleLabel.text = @"个人主页";
}- (void)viewDidLoad {[super viewDidLoad];self.edgesForExtendedLayout = UIRectEdgeNone;// Do any additional setup after loading the view.self.view.backgroundColor = kBgColor;[self.pagingView reload];/* 需要设置self.edgesForExtendedLayout = UIRectEdgeNone; 最好自定义导航栏* 在代理 - (void)pagingView:(HHHorizontalPagingView *)pagingView scrollTopOffset:(CGFloat)offset*做出对应处理来改变 背景色透明度*/self.navView = [[ArtNavView alloc] init];CGSize size = [UIScreen mainScreen].bounds.size;self.navView.frame = CGRectMake(0, 0, size.width, kNavBarHeight);[self.view addSubview:self.navView];[self.navView.leftBut addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
}- (void)back {[self.navigationController popViewControllerAnimated:YES];
}- (void)viewWillDisappear:(BOOL)animated {[self.navigationController setNavigationBarHidden:NO animated:animated];[super viewWillDisappear:animated];
}- (void)showText:(NSString *)str {UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:str delegate: self cancelButtonTitle:nil otherButtonTitles:nil];[alert show];dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{[alert dismissWithClickedButtonIndex:0 animated:YES];});
}#pragma mark - HHHorizontalPagingViewDelegate
// 下方左右滑UIScrollView设置
- (NSInteger)numberOfSectionsInPagingView:(HHHorizontalPagingView *)pagingView {return 2;
}- (UIScrollView *)pagingView:(HHHorizontalPagingView *)pagingView viewAtIndex:(NSInteger)index{HomePageContentTableVC *vc = [[HomePageContentTableVC alloc] init];[self.contentVCArrays addObject:vc];[self addChildViewController:vc];vc.index = index;vc.fillHight = self.pagingView.segmentTopSpace + 45;return (UIScrollView *)vc.view;
}//headerView 设置
- (CGFloat)headerHeightInPagingView:(HHHorizontalPagingView *)pagingView {return kHeaderTopHeight;
}- (UIView *)headerViewInPagingView:(HHHorizontalPagingView *)pagingView {return self.headerView;
}- (void)but1Click{NSLog(@"%s",__func__);[self showText:@"but1Click"];
}- (void)but2Click{NSLog(@"%s",__func__);[self showText:@"but2Click"];
}//segmentButtons
- (CGFloat)segmentHeightInPagingView:(HHHorizontalPagingView *)pagingView {return 45.;
}- (NSArray<UIButton*> *)segmentButtonsInPagingView:(HHHorizontalPagingView *)pagingView {NSMutableArray *buttonArray = [NSMutableArray array];for(int i = 0; i < 2; i++) {UIButton *segmentButton = [UIButton buttonWithType:UIButtonTypeCustom];[segmentButton setBackgroundImage:[UIImage imageNamed:@"Home_title_line"] forState:UIControlStateNormal];[segmentButton setBackgroundImage:[UIImage imageNamed:@"Home_title_line_select"] forState:UIControlStateSelected];NSString *str = i == 0 ? @"帖子" : @"评论";[segmentButton setTitle:str forState:UIControlStateNormal];[segmentButton setTitleColor:GrayColor(54) forState:UIControlStateNormal];[segmentButton setTitleColor:kThemeColor forState:UIControlStateSelected];segmentButton.adjustsImageWhenHighlighted = NO;[buttonArray addObject:segmentButton];}self.buttonArray = [NSMutableArray arrayWithArray:buttonArray];return [buttonArray copy];
}// 点击segment
- (void)pagingView:(HHHorizontalPagingView*)pagingView segmentDidSelected:(UIButton *)item atIndex:(NSInteger)selectedIndex{NSLog(@"%s",__func__);NSLog(@"将要显示的类别:%zd",selectedIndex);}- (void)pagingView:(HHHorizontalPagingView*)pagingView segmentDidSelectedSameItem:(UIButton *)item atIndex:(NSInteger)selectedIndex{NSLog(@"%s",__func__);}// 视图切换完成时调用
- (void)pagingView:(HHHorizontalPagingView*)pagingView didSwitchIndex:(NSInteger)aIndex to:(NSInteger)toIndex{NSLog(@"%s \n %tu to %tu",__func__,aIndex,toIndex);HomePageContentTableVC *contentTableVC = self.contentVCArrays[toIndex];//判断是否有网络Reachability *reach = [Reachability reachabilityForInternetConnection];NetworkStatus status = [reach currentReachabilityStatus];if(status == NotReachable)//无网,从数据库取出后赋值{[MBProgressHUD showError:@"当前网络不佳!"];}else//请求数据{//NSMutableArray *temp = [NSMutableArray array];for (int i=0; i<10; i++) {NSString *str = [NSString stringWithFormat:@"数据:%zd",i];[temp addObject:str];}//刷新contentTableVC.modelArray = [NSArray arrayWithArray:temp];}}- (void)pagingView:(HHHorizontalPagingView *)pagingView scrollTopOffset:(CGFloat)offset {if (offset >= -kNavBarHeight - 45) { // > 0 代表已经只顶了return;}CGFloat fm = self.pagingView.pullOffset - kNavBarHeight - 45;CGFloat fz = - kNavBarHeight - 45 - offset;float al = 1.0 - fz / fm;al = al <= 0.05 ? 0 : al;al = al >= 0.95 ? 1 : al;// NSLog(@"__ %f __ %f __ %lf",offset,self.pagingView.pullOffset, al);[self.navView changeAlpha:al];
}- (void)didReceiveMemoryWarning {[super didReceiveMemoryWarning];// Dispose of any resources that can be recreated.
}@end
创建控制器HomePageContentTableVC.h
//
// HomePageContentTableVC.h
// lingfo
//
// Created by limin on 16/12/28.
// Copyright © 2016年 anxin. All rights reserved.
//#import <UIKit/UIKit.h>@interface HomePageContentTableVC : UIViewController@property (nonatomic, strong, readonly) UITableView *tableView;
@property (nonatomic, assign) NSInteger index;
@property (nonatomic, assign) BOOL allowPullToRefresh;
@property (nonatomic, assign) CGFloat pullOffset;
@property (nonatomic, assign) CGFloat fillHight; //segmentButtons + segmentTopSpace/* 数据模型 */
@property(nonatomic,strong)NSArray *modelArray;@end
创建控制器HomePageContentTableVC.m
//
// HomePageContentTableVC.m
// lingfo
//
// Created by limin on 16/12/28.
// Copyright © 2016年 anxin. All rights reserved.
//#import "HomePageContentTableVC.h"
#import "HHHorizontalPagingView.h"
#import "UIView+WhenTappedBlocks.h"#import "SVPullToRefresh.h"
#import "LMMyHomeController.h"
@interface HomePageContentTableVC()<UITableViewDelegate,UITableViewDataSource>@property (nonatomic, strong) UITableView *homePageTableView;@end@implementation HomePageContentTableVC
#pragma mark - 懒加载- (void)loadView{self.homePageTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];self.homePageTableView.dataSource = self;self.homePageTableView.delegate = self;[self.homePageTableView setSeparatorColor:GrayColor(231)];self.homePageTableView.backgroundColor = kBgColor;self.view = self.homePageTableView;//去掉底部多余的表格线[self.homePageTableView setTableFooterView:[[UIView alloc] initWithFrame:CGRectZero]];[self solvePlainSeparatorStyle];[self setupMsgRefresh];
}
-(void)solvePlainSeparatorStyle
{//1.解决Cell下面的线左边15像素空白问题if ([self.homePageTableView respondsToSelector:@selector(setSeparatorInset:)]){[self.homePageTableView setSeparatorInset:UIEdgeInsetsZero];}if ([self.homePageTableView respondsToSelector:@selector(setLayoutMargins:)]){[self.homePageTableView setLayoutMargins:UIEdgeInsetsZero];}
}
/** 添加刷新控件*/
-(void)setupMsgRefresh
{//下拉刷新控件self.homePageTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewDynamic)];[self loadNewDynamic];//自定义footerself.homePageTableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreDynamic)];}
-(void)loadNewDynamic
{[self.homePageTableView.mj_footer endRefreshing];//无网络状态也需要赋值(之前保存在偏好设置里面的数据)Reachability *reach = [Reachability reachabilityForInternetConnection];NetworkStatus status = [reach currentReachabilityStatus];if (status == NotReachable) {//无网络[MBProgressHUD showError:@"请检查网络后重试"];}else//网络请求{NSMutableArray *temp = [NSMutableArray array];for (int i=0; i<10; i++) {NSString *str = [NSString stringWithFormat:@"数据:%zd",i];[temp addObject:str];}//结束刷新状态[self.homePageTableView.mj_header endRefreshing];//刷新控件self.modelArray = temp;//控制footer的状态if (temp.count==0) {//没有更多数据了self.homePageTableView.mj_footer.hidden = YES;}else if (temp.count < 10) {//全部加载完毕[self.homePageTableView.mj_footer endRefreshingWithNoMoreData];}}}
-(void)loadMoreDynamic
{[self.homePageTableView.mj_header endRefreshing];//无网络状态也需要赋值(之前保存在偏好设置里面的数据)Reachability *reach = [Reachability reachabilityForInternetConnection];NetworkStatus status = [reach currentReachabilityStatus];if (status == NotReachable) {//无网络[MBProgressHUD showError:@"请检查网络后重试"];}else//网络请求{NSMutableArray *totalArray = [NSMutableArray arrayWithArray:self.modelArray];NSMutableArray *tempArray = [NSMutableArray array];for (int i=0; i<10; i++) {NSString *str = [NSString stringWithFormat:@"数据:%zd",i];[tempArray addObject:str];}//控制footer的状态if (tempArray.count < 10) {//全部加载完毕[self.homePageTableView.mj_footer endRefreshingWithNoMoreData];}else{//结束刷新状态[self.homePageTableView.mj_footer endRefreshing];}//最新评论[totalArray addObjectsFromArray:tempArray];self.modelArray = totalArray;}
}
-(void)setModelArray:(NSArray *)modelArray
{_modelArray = modelArray;[self.homePageTableView reloadData];
}
- (void)viewDidLoad{[super viewDidLoad];if (!self.allowPullToRefresh) {return;}[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(takeBack:) name:kHHHorizontalTakeBackRefreshEndNotification object:self.tableView];__weak typeof(self)weakSelf = self;[self.tableView addPullToRefreshOffset:self.pullOffset withActionHandler:^{[[NSNotificationCenter defaultCenter] postNotificationName:kHHHorizontalScrollViewRefreshStartNotification object:weakSelf.tableView userInfo:nil];dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{[weakSelf.tableView.pullToRefreshView stopAnimating];[[NSNotificationCenter defaultCenter] postNotificationName:kHHHorizontalScrollViewRefreshEndNotification object:weakSelf.tableView userInfo:nil];});}];
}- (void)takeBack:(NSNotification *)noti{[self.tableView.pullToRefreshView stopAnimating:NO];
}- (void)dealloc{NSLog(@"%s",__func__);
}#pragma mark - UITableViewDataSource
- (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.textLabel.textColor = kThemeColor;}cell.textLabel.text = [NSString stringWithFormat:@"%@%@",self.index?@"评论":@"帖子",self.modelArray[indexPath.row]];return cell;
}- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {return 1;
}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {self.homePageTableView.mj_footer.hidden = (self.modelArray.count == 0);if (self.modelArray.count < 10 && self.modelArray.count > 0) {//全部加载完毕[self.homePageTableView.mj_footer endRefreshingWithNoMoreData];}return self.modelArray.count;}- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {return 80;}#pragma mark - <点击进去详情页>
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{[self.homePageTableView deselectRowAtIndexPath:indexPath animated:YES];}- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{return 0.0001;
}- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{// 通过最后一个 Footer 来补高度if (section == [self numberOfSectionsInTableView:tableView] - 1) {return [self automaticHeightForTableView:tableView];}return 0.0001;
}- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{UIView *view = [[UIView alloc] init];view.backgroundColor = kBgColor;return view;
}- (CGFloat)automaticHeightForTableView:(UITableView *)tableView{// 36 是 segmentButtons 的高度 20 是segmentTopSpace的高度CGFloat height = self.fillHight;NSInteger section = [tableView.dataSource numberOfSectionsInTableView:tableView];for (int i = 0; i < section; i ++) {if ([tableView.delegate respondsToSelector:@selector(tableView:heightForHeaderInSection:)]) {height += [tableView.delegate tableView:tableView heightForHeaderInSection:i];}NSInteger row = [tableView.dataSource tableView:tableView numberOfRowsInSection:i];for (int j= 0 ; j < row; j++) {NSIndexPath *indexPath = [NSIndexPath indexPathForRow:j inSection:i];if ([tableView.delegate respondsToSelector:@selector(tableView:heightForRowAtIndexPath:)]) {height += [tableView.delegate tableView:tableView heightForRowAtIndexPath:indexPath];}if (height >= tableView.frame.size.height) {return 0.0001;}}if (i != section - 1) {if ([tableView.delegate respondsToSelector:@selector(tableView:heightForFooterInSection:)]) {height += [tableView.delegate tableView:tableView heightForFooterInSection:i];}}}if (height >= tableView.frame.size.height) {return 0.0001;}return tableView.frame.size.height - height;
}
@end
详细项目链接:Github地址: https://github.com/MinLee6/WeiboUserHomePage