//
|
// TWScontroller.m
|
// BuWanVideo2.0
|
//
|
// Created by weikou2016 on 16/8/18.
|
// Copyright © 2016年 com.yeshi.buwansheque.ios. All rights reserved.
|
//
|
|
#import "TWScontroller.h"
|
#import "TWScell.h"
|
#import "WebControllerView.h"
|
|
@interface mytable : UITableView
|
@property(nonatomic,assign)int page;
|
@property(nonatomic,strong)NSMutableArray *data;
|
@property(nonatomic,assign)int type;
|
@end
|
@implementation mytable
|
@synthesize page;
|
@synthesize type;
|
@synthesize data;
|
@end
|
|
|
|
@interface TWScontroller ()<UITableViewDataSource,UITableViewDelegate,UIScrollViewDelegate>{
|
NSMutableArray * data; //type 类型数据源
|
HMSegmentedControl * segmentedControl;
|
UIScrollView * ScrollView;
|
NSMutableArray * tableviewarray; //所有的table
|
int indext; //当前tablew位置
|
NSMutableArray *arrname; //类型名字
|
}
|
|
@end
|
|
@implementation TWScontroller
|
|
- (void)viewDidLoad {
|
[super viewDidLoad];
|
|
|
[self initS];
|
[self initTypedata];
|
}
|
|
-(void)dealloc{
|
|
}
|
|
-(void)initS{
|
self.navigationItem.title = @"图文社";
|
|
self.navigationController.navigationBar.titleTextAttributes = @{NSFontAttributeName:[UIFont systemFontOfSize:18],NSForegroundColorAttributeName:[UIColor whiteColor]};
|
|
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
|
[button setImage:[[UIImage imageNamed:@"详情页面返回"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
|
[button addTarget:self action:@selector(back:)
|
forControlEvents:UIControlEventTouchUpInside];
|
button.frame = CGRectMake(0, 0, 32, 32);
|
UIBarButtonItem *iconBarItem=[[UIBarButtonItem alloc] initWithCustomView:button];
|
self.navigationItem.leftBarButtonItem = iconBarItem;
|
|
self.view.backgroundColor = [UIColor whiteColor];
|
|
UIView *v = [[UIView alloc]initWithFrame:CGRectMake(0, kNavigationBarH, KScreenW, 40)];
|
v.backgroundColor = kGlobalMainColor;
|
v.tag = 123;
|
[self.view addSubview:v];
|
}
|
|
//获得数据
|
-(void)initTypedata{
|
[[YTHNetInterface startInterface] getFoundGraphicSociety:[YTHsharedManger startManger].Uid WithSystem:@"1" WithBlock:^(BOOL isSuccessful, id result, NSString *error) {
|
if(isSuccessful){
|
data = [[result objectForKey:@"Data"] objectForKey:@"data"];
|
|
if(data.count > 0){
|
arrname = [NSMutableArray new];
|
for (int i = 0 ; i < data.count; ++i) {
|
[arrname addObject:[data[i] objectForKey:@"Name"]];
|
}
|
indext = 0;
|
[self initScene]; //获得了数据创建下面的tablewview
|
mytable *table = tableviewarray[indext];
|
[table.mj_header beginRefreshing];
|
}
|
}else{
|
NSLog(@"%@",error);
|
}
|
|
}];
|
}
|
|
//获得了资讯typy 后创建
|
-(void)initScene{
|
|
segmentedControl= [[HMSegmentedControl alloc] initWithSectionTitles:arrname];
|
segmentedControl.backgroundColor=kGlobalMainColor;
|
segmentedControl.selectionIndicatorHeight = 2.0f;
|
segmentedControl.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth;
|
segmentedControl.frame = CGRectMake(0, kNavigationBarH, KScreenW, 40);
|
segmentedControl.segmentEdgeInset = UIEdgeInsetsMake(0, 10, 0, 10);
|
segmentedControl.selectionStyle = HMSegmentedControlSelectionStyleFullWidthStripe;
|
segmentedControl.selectionIndicatorLocation = HMSegmentedControlSelectionIndicatorLocationDown;
|
segmentedControl.selectedSegmentIndex = 0;
|
segmentedControl.titleTextAttributes = @{NSForegroundColorAttributeName : [UIColor whiteColor],NSFontAttributeName :[UIFont systemFontOfSize:15]};
|
segmentedControl.selectedTitleTextAttributes=@{NSForegroundColorAttributeName : kGlobalYellowColor,NSFontAttributeName :[UIFont systemFontOfSize:15]};
|
segmentedControl.selectionIndicatorColor = kGlobalYellowColor;
|
[self.view addSubview:segmentedControl];
|
|
[segmentedControl setIndexChangeBlock:^(NSInteger index) {
|
indext = (int)index;
|
[ScrollView scrollRectToVisible:CGRectMake(KScreenW * index, kNavigationBarH+40, KScreenW, KScreenH-kNavigationBarH-40) animated:YES];
|
mytable *table = tableviewarray[indext];
|
[table.mj_header beginRefreshing];
|
}];
|
|
[[self.view viewWithTag:123] removeFromSuperview];
|
|
//配置滑动视图
|
ScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, kNavigationBarH+40, KScreenW, KScreenH-kNavigationBarH-40)];
|
ScrollView.pagingEnabled = YES;
|
ScrollView.showsVerticalScrollIndicator = YES;
|
ScrollView.showsHorizontalScrollIndicator = NO;
|
ScrollView.contentSize = CGSizeMake(KScreenW * data.count, KScreenH-kNavigationBarH-40);
|
ScrollView.delegate = self;
|
[ScrollView scrollRectToVisible:CGRectMake(0, 0, KScreenW, KScreenH-kNavigationBarH-40) animated:NO];
|
[self.view addSubview: ScrollView];
|
|
tableviewarray = [NSMutableArray new];
|
for (int i = 0 ; i < data.count ; i++) {
|
mytable *tableview = [[mytable alloc]initWithFrame:CGRectMake(KScreenW * i, 0, KScreenW,KScreenH - (segmentedControl.frame.size.height + segmentedControl.frame.origin.y)) style:UITableViewStylePlain];
|
tableview.delegate = self;
|
tableview.page = 1;
|
tableview.type = ((NSString*)[data[i] objectForKey:@"Id"]).intValue;
|
[tableviewarray addObject:tableview];
|
|
tableview.dataSource = self;
|
tableview.backgroundColor = [UIColor whiteColor];
|
tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
|
[ScrollView addSubview:tableview];
|
[tableview registerNib:[UINib nibWithNibName:@"TWScell" bundle:nil] forCellReuseIdentifier:@"TWScell"];
|
|
tableview.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headupdata)];
|
tableview.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footupdata)];
|
}
|
}
|
|
//初始化
|
-(void)headupdata{
|
mytable *table = tableviewarray[indext];
|
NSLog(@"%d = page",indext);
|
table.page = 1;
|
[[YTHNetInterface startInterface] getFoundLinefornew:[YTHsharedManger startManger].Uid withPage:@"1" withType:[NSString stringWithFormat:@"%d",table.type] WithSystem:@"1" WithBlock:^(BOOL isSuccessful, id result, NSString *error) {
|
if(isSuccessful){
|
table.data = [[result objectForKey:@"Data"]objectForKey:@"data"];
|
if(table.data .count >0){
|
[table reloadData];
|
table.page+=1;
|
}
|
}else{
|
NSLog(@"%@",error);
|
}
|
[table.mj_header endRefreshing];
|
}];
|
}
|
|
-(void)footupdata{
|
mytable *table = tableviewarray[indext];
|
NSLog(@"%d = indext",indext);
|
NSLog(@"%d = page",table.page);
|
[[YTHNetInterface startInterface] getFoundLinefornew:[YTHsharedManger startManger].Uid withPage:[NSString stringWithFormat:@"%d",table.page] withType:[NSString stringWithFormat:@"%d",table.type] WithSystem:@"1" WithBlock:^(BOOL isSuccessful, id result, NSString *error) {
|
if(isSuccessful){
|
NSArray *newarr = [[result objectForKey:@"Data"] objectForKey:@"data"];
|
if(newarr.count > 0){
|
[table.data addObjectsFromArray:newarr];
|
[table reloadData];
|
table.page+=1;
|
}
|
}else{
|
NSLog(@"%@",error);
|
}
|
[table.mj_footer endRefreshing];
|
}];
|
}
|
|
-(void)back:(UIButton*)sender{
|
[self.navigationController popToRootViewControllerAnimated:YES];
|
}
|
|
#pragma mark - UIScrollViewDelegate
|
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
|
CGFloat pageWidth =KScreenW;
|
NSInteger page = ScrollView.contentOffset.x / pageWidth;
|
if(indext != page){
|
indext = (int)page;
|
[segmentedControl setSelectedSegmentIndex:page animated:YES];
|
UITableView *table = tableviewarray[indext];
|
[table.mj_header beginRefreshing];
|
}
|
}
|
|
#pragma mark UITableViewDataSource UITableViewDelegate
|
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
|
mytable * table = tableviewarray[indext];
|
return table.data.count;
|
}
|
|
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
|
return 100;
|
}
|
|
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
|
TWScell * cell = [tableView dequeueReusableCellWithIdentifier:@"TWScell"];
|
if([self IsNewlineWithString:cell.title.text WithString:cell.title.frame.size.width]){
|
cell.title.text = [NSString stringWithFormat:@"%@\n",cell.title.text];
|
}
|
|
mytable *table = tableviewarray[indext];
|
NSDictionary *Dic = table.data[indexPath.row];
|
NSDictionary *image =[Dic objectForKey:@"ImgList"][0];
|
[cell.image setYthImageWithURL:[image objectForKey:@"Url"] placeholderImage:[UIImage imageNamed:@"默认加载图片"]];
|
cell.title.text = [Dic objectForKey:@"Title"];
|
cell.comefrom.text = [Dic objectForKey:@"From"];
|
cell.time.text = [Dic objectForKey:@"Date"];
|
//[self gettime:[Dic objectForKey:@"Date"]];
|
|
float look = ((NSString*)[Dic objectForKey:@"ReadCount"]).floatValue;
|
if(look+0.01 >= 10000){
|
cell.play.text = [NSString stringWithFormat:@"%.2f万",look/10000];
|
}else{
|
cell.play.text = [NSString stringWithFormat:@"%.0f",look];
|
}
|
cell.selectionStyle = UITableViewCellSelectionStyleNone;
|
return cell;
|
}
|
|
-(NSString*)gettime:(NSString*)time{
|
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
|
[dateFormatter setDateFormat:@"YYYY-MM-dd"];
|
NSString *dateString = [dateFormatter stringFromDate:[NSDate date]];
|
|
NSArray *now = [dateString componentsSeparatedByString:@"-"];
|
NSArray *get = [time componentsSeparatedByString:@"-"];
|
|
int year = ((NSString*)now[0]).intValue - ((NSString*)get[0]).intValue;
|
int month = ((NSString*)now[1]).intValue - ((NSString*)get[1]).intValue;
|
int day = ((NSString*)now[2]).intValue - ((NSString*)get[2]).intValue;
|
|
int all = year * 365 + month * 30 + day;
|
|
NSMutableString *str = [NSMutableString new];
|
if ( all == 0) {
|
str = (NSMutableString*)@"刚刚";
|
}else if(all < 31){
|
str = (NSMutableString*)[NSString stringWithFormat:@"%d天前",all];
|
}else if(all < 365){
|
str = (NSMutableString*)[NSString stringWithFormat:@"%d月前",all/31];
|
}else{
|
str = (NSMutableString*)[NSString stringWithFormat:@"%d年前",all/365];
|
}
|
return str;
|
}
|
|
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
|
WebControllerView * web = [WebControllerView new];
|
mytable *table = tableviewarray[indext];
|
NSDictionary *dic = table.data[indexPath.row];
|
web.url = [NSURL URLWithString:[dic objectForKey:@"Url"]];
|
[self.navigationController pushViewController:web animated:YES];
|
}
|
|
/**
|
* 是否需要换行 是在字符串后加\n
|
*/
|
-(BOOL)IsNewlineWithString:(NSString*)str WithString:(float)width{
|
UILabel *lb = [UILabel new];
|
lb.text = str;
|
lb.numberOfLines = 0;
|
lb.font =[UIFont systemFontOfSize:12];
|
CGSize size = [lb sizeThatFits:CGSizeMake(width, MAXFLOAT)];
|
if(size.height > 15){
|
return NO;
|
}else{
|
return YES;
|
}
|
}
|
|
@end
|