admin
2023-04-21 0b3a4aaf99ea251bc8e27b96115288f0988fcffe
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
//
//  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