//#import "ViewController.h"
#import "MyCollectionViewCell.h"
#import "Story.pch"
#import "WBPopMenuModel.h"
#import "WBPopMenuSingleton.h"#define BTN_W FIT_X(80) // 每个标题按钮的宽度@interface ViewController (){
NSMutableArray *_allTitles; //所有的新闻标题
NSArray *_allDatas; //说有的新闻数据
NSArray *_tableDatas; //给每一个网格单元格中的表格视图加载数据的数组
}
@property (nonatomic,strong)UIScrollView *titleScrView;//标题滚动视图
@property (nonatomic,strong)UILabel *titleIndicateLable; //标题下方的指示标签
@property (nonatomic,strong)UICollectionView *contentsColView; //新闻内容网格视图
@end
@implementation ViewController
#pragma mark - 懒加载控件
//标题滚动视图
- (UIScrollView *)titleScrView
{
if (!_titleScrView)
{
_titleScrView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, FIT_Y(64), SCR_W, FIT_Y(44))];
_titleScrView.contentSize = CGSizeMake(BTN_W * _allTitles.count, FIT_Y(44));
_titleScrView.showsHorizontalScrollIndicator = NO;
for (int i = 0 ; i < _allTitles.count; i++)
{
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(i*BTN_W, 0, BTN_W, FIT_Y(44));
[btn setTitle:_allTitles[i] forState:UIControlStateNormal];
[btn setTitle:_allTitles[i] forState:UIControlStateSelected];
[btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
[btn setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
if (i == 0) {
btn.selected = YES;
}
btn.tag = i+100;
[btn addTarget:self action:@selector(titleBtnHandle:) forControlEvents:UIControlEventTouchUpInside];
[_titleScrView addSubview:btn];
}
[_titleScrView addSubview:self.titleIndicateLable];
}
return _titleScrView;
}
//标题指示标签
- (UILabel *)titleIndicateLable
{
if (!_titleIndicateLable)
{
_titleIndicateLable = [[UILabel alloc]initWithFrame:CGRectMake(0, FIT_Y(44), BTN_W, FIT_Y(2))];
_titleIndicateLable.backgroundColor = [UIColor redColor];
}
return _titleIndicateLable;
}
//新闻内容视图
- (UICollectionView *)contentsColView
{
if (!_contentsColView)
{
//实例化网格布局对象
UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc]init];
//设置每个单元格大小
flow.itemSize = CGSizeMake(SCR_W, SCR_H-FIT_Y(44));
//设置最小行间距
flow.minimumLineSpacing = 0;
//设置最小列间距
flow.minimumInteritemSpacing = 0;
//设置滚动方向水平滚动
flow.scrollDirection = UICollectionViewScrollDirectionHorizontal;
//实例化网格视图
_contentsColView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, FIT_Y(110), SCR_W, SCR_H-FIT_Y(44)) collectionViewLayout:flow];
_contentsColView.tag = 444;
_contentsColView.dataSource = self;
_contentsColView.delegate = self;
_contentsColView.pagingEnabled = YES; //分页滚动
//注册一个cell
[_contentsColView registerClass:[MyCollectionViewCell class] forCellWithReuseIdentifier:@"NewsCell"];
}
return _contentsColView;
}
//标题滚动button方法
- (void)titleBtnHandle:(UIButton *)btn
{
//点击按钮让collection滚动
[self.contentsColView scrollRectToVisible:CGRectMake((btn.tag-100)*SCR_W, 0, SCR_W, FIT_Y(44)) animated:YES];
}
#pragma mark - UICollectionViewDataSource
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return _allTitles.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = @"NewsCell";
MyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
if (!cell)
{
cell = [[MyCollectionViewCell alloc]initWithFrame:CGRectMake(0, 0, SCR_W, SCR_H-FIT_Y(44))];
}
cell.newsTable.dataSource = self;
cell.newsTable.delegate = self;
return cell;
}
#pragma mark - UICollectionViewDelegate
//将要出现cell时回调的方法
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
//如果内容网格视图滚动,执行if中的代码
if (scrollView.tag == 444)
{
int index = scrollView.contentOffset.x / SCR_W;
//其他的button变为normal颜色
for (int i = 0; i < _allTitles.count ; i++)
{
UIButton *btn = (UIButton *)[self.titleScrView viewWithTag:i+100];
btn.selected = NO;
if (index == i)
{
btn.selected = YES;
}
}
//移动指示标签
[UIView animateWithDuration:0.185 animations:^{
self.titleIndicateLable.frame = CGRectMake(index * BTN_W, FIT_Y(42), BTN_W, FIT_Y(2));
}];
//让标题滚动视图滚动到指定位置
[self.titleScrView scrollRectToVisible:CGRectMake(BTN_W*index, 0, BTN_W, FIT_Y(44)) animated:YES];
}
}
//即将滚动出现cell时的回调方法
-(void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"%ld",indexPath.row);
//改变网格单元格中表格视图的内容
_tableDatas = _allDatas[indexPath.row];
MyCollectionViewCell *newsCell = (MyCollectionViewCell *)cell;
[newsCell.newsTable reloadData];
}
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return _tableDatas.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (!cell)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
cell.textLabel.text = _tableDatas[indexPath.row];
return cell;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor whiteColor];
// _allTitles = @[@"移动通讯",@"传媒",@"软工",@"网工",@"云计算",@"建筑",@"网站",@"高翻",@"网络营销",@"基础"];
_allTitles = [NSMutableArray arrayWithObjects:@"移动通讯",@"传媒",@"软工",@"网工",@"云计算",@"建筑",@"网站",@"高翻",@"网络营销",@"基础", nil];
_allDatas = @[@[@"iOS苹果系",@"安卓系",@"测试系"],
@[@"网站",@"UI",@"后台"],
@[@"软工1",@"体育2",@"体育3"],
@[@"网工",@"科技2",@"科技3"],
@[@"云计算",@"财经2",@"财经3"],
@[@"建筑",@"军事2",@"军事3"],
@[@"网站",@"娱乐2",@"娱乐3"],
@[@"高翻",@"家居2",@"家居3"],
@[@"网络营销"],
@[@"男子基础",@"女子基础"]
];
_tableDatas = _allDatas[0];
UIView *v = [[UIView alloc]init];
UIBarButtonItem *rightBarItem = [[UIBarButtonItem alloc] initWithTitle:@"+" style:UIBarButtonItemStylePlain target:self action:@selector(onClickedOKbtn)];
self.navigationItem.rightBarButtonItem = rightBarItem;
[self.view addSubview: v];
[self.view addSubview:self.titleScrView];
[self.view addSubview:self.contentsColView];
}
//响应事件代码:
- (void)onClickedOKbtn {
NSMutableArray *obj = [NSMutableArray array];
for (NSInteger i = 0; i < [self titles].count; i++) {
WBPopMenuModel * info = [WBPopMenuModel new];
info.title = [self titles][i];
[obj addObject:info];
}
[[WBPopMenuSingleton shareManager]showPopMenuSelecteWithFrame:150
item:obj
action:^(NSInteger index) {
NSLog(@"index:%ld",(long)index);
UIAlertView *alertview = [[UIAlertView alloc] initWithTitle:@"提示" message:@"添加成功" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"好的", nil];
[alertview show];
}];
}
- (NSArray *) titles {
return @[@"确认添加",
@"删除添加",
@"关闭"
];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
#import@interface MyCollectionViewCell : UICollectionViewCell
@property (nonatomic,strong)UITableView *newsTable;
#import "MyCollectionViewCell.h"
#import "Story.pch"
@implementation MyCollectionViewCell
#pragma Mark - 重写
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
self.newsTable = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCR_W, SCR_H-FIT_Y(44)) style:UITableViewStylePlain];
[self.contentView addSubview:self.newsTable];
}
return self;
}
@end