-
Notifications
You must be signed in to change notification settings - Fork 100
/
Copy pathHelpNewsViewController.m
320 lines (264 loc) · 11.2 KB
/
HelpNewsViewController.m
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
//
// HelpNewsViewController.m
// DSBBM
//
// Created by bisheng on 14-9-18.
// Copyright (c) 2014年 qt. All rights reserved.
//
#import "HelpNewsViewController.h"
#import "HelpNewsModel.h"
#import "HelpNewsTableViewCell.h"
#import "DetailWishViewController.h"
#import "WishModel.h"
@interface HelpNewsViewController ()
@end
@implementation HelpNewsViewController
{
UITableView * helpTableView;
NSMutableArray * newsArray;
NSMutableArray * wishArray;
MJRefreshHeaderView *_header;
MJRefreshFooterView *_footer;
MBProgressHUD * HUD;
int _skip;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
newsArray = [NSMutableArray array];
wishArray = [NSMutableArray array];
_skip = 0;
}
return self;
}
- (void)dealloc
{
[_header removeFromSuperview];
[_footer removeFromSuperview];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
[self createNaBar];
[self createTableView];
[self getData:_skip];
NSNotification * center = [[NSNotification alloc] initWithName:@"detailsNews" object:nil userInfo:nil];
[[NSNotificationCenter defaultCenter] postNotification:center];
}
#pragma mark tableView
-(void)createTableView{
helpTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, 320, self.view.bounds.size.height-64)];
helpTableView.delegate = self;
helpTableView.dataSource = self;
helpTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
[self.view addSubview:helpTableView];
// 刷新功能
_header = [[MJRefreshHeaderView alloc] init];
_header.delegate = self;
_header.scrollView = helpTableView;
//添加上拉加载更多
_footer = [[MJRefreshFooterView alloc] init];
_footer.delegate = self;
_footer.scrollView = helpTableView;
HUD= [[MBProgressHUD alloc] initWithFrame: CGRectMake(0, 0, 320, 320)];
HUD.labelText = @"加载中";
[self.view addSubview:HUD];
[HUD show:YES];
}
#pragma mark 代理方法-进入刷新状态就会调用
- (void)refreshViewBeginRefreshing:(MJRefreshBaseView *)refreshView
{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"HH : mm : ss.SSS";
if (_header == refreshView)
{
//下拉刷新数据请求********************************
// 2秒后刷新表格
[self performSelector:@selector(refreshDate) withObject:nil afterDelay:2];
} else {
//上拉刷新数据请求**************************************
// 2秒后刷新表格
[self performSelector:@selector(reloadDeals) withObject:nil afterDelay:2];
}
}
-(void)refreshDate{
_skip = 0;
[self getData:_skip];
}
- (void)reloadDeals
{
_skip = _skip + 15;
[self getData:_skip];
}
#pragma mark tableView 方法
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
HelpNewsTableViewCell * newscell = [[HelpNewsTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"news"];
if(!newscell){
newscell = [[[NSBundle mainBundle] loadNibNamed:@"news" owner:self options:nil] lastObject];
}
HelpNewsModel * news = newsArray[indexPath.row];
CGSize size = [news.text boundingRectWithSize:CGSizeMake(180, 999) options: NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:17]} context:nil].size;
if(size.height < 20){
size.height = 20;
}
newscell.nick.text = news.nick;
if([news.msgType integerValue] == 1){
[newscell addSubview:newscell.like];
}else{
newscell.text.frame = CGRectMake(75, 33,180, size.height);
newscell.text.text = news.text;
}
if([[news.time substringToIndex:1] integerValue] == 0){
newscell.time.text = @"1分钟前";
}else{
newscell.time.text = news.time;
}
newscell.time.frame = CGRectMake(75,size.height + 32, 100, 12);
newscell.wish.frame = CGRectMake(260, 15,45, 45);
newscell.wish.text = news.wish;
NSURL * avatarimu = [NSURL URLWithString:news.avatar];
[newscell.avatar sd_setImageWithURL:avatarimu];
NSURL * wishimu = [NSURL URLWithString:news.wishImage];
[newscell.wishImage sd_setImageWithURL:wishimu];
if(size.height < 20){
newscell.line.frame = CGRectMake(15, 79.5, 305, 0.5);
}else{
newscell.line.frame = CGRectMake(15, size.height + 59.5, 305, 0.5);
}
return newscell;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return newsArray.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
HelpNewsModel * news = newsArray[indexPath.row];
CGSize size = [news.text boundingRectWithSize:CGSizeMake(180, 999) options: NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:17]} context:nil].size;
if(size.height < 20)
return 80;
return size.height + 60;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
DetailWishViewController * detailwish = [[DetailWishViewController alloc] init];
WishModel * wish = wishArray[indexPath.row];
detailwish.objectId = wish.obj.objectId;
[tableView deselectRowAtIndexPath:indexPath animated:NO];
[self.navigationController pushViewController:detailwish animated:YES];
}
#pragma mark data
-(void)getData:(int)skip{
BmobUser * bUser = [BmobUser getCurrentObject];
BmobQuery *wishBquery = [BmobQuery queryWithClassName:@"WishMsg"];
[wishBquery whereKey:@"toUser" equalTo:bUser];
[wishBquery orderByDescending:@"createdAt"];
wishBquery.limit = 15;
wishBquery.skip = skip;
[wishBquery includeKey:@"fromUser,toUser,wish"];
[wishBquery findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error){
if(error){
HUD.labelText = @"载入失败";
HUD.mode = MBProgressHUDModeCustomView;
HUD.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Checkmark"]];
[HUD showAnimated:YES whileExecutingBlock:^{
sleep(1);
} completionBlock:^{
[HUD removeFromSuperview];
HUD = nil;
}];
}else{
[HUD removeFromSuperview];
if(_skip == 0){
[newsArray removeAllObjects];
}
for(BmobObject * obj in array){
HelpNewsModel * news = [[HelpNewsModel alloc] init];
news.nick = [[obj objectForKey:@"fromUser"] objectForKey:@"nick"];
news.text = [obj objectForKey:@"content"];
news.msgType = [obj objectForKey:@"msgType"];
news.time = [mistiming returnUploadTime:[NSString stringWithFormat:@"%@",obj.createdAt]];
news.avatar = [[obj objectForKey:@"fromUser"] objectForKey:@"avatar"];
news.wish = [[obj objectForKey:@"wish"] objectForKey:@"text"];
news.wishImage = [[obj objectForKey:@"wish"] objectForKey:@"url"];
[newsArray addObject:news];
WishModel * wish = [[WishModel alloc] init];
wish.obj = [obj objectForKey:@"wish"];
wish.name = [[obj objectForKey:@"toUser"] objectForKey:@"nick"];
wish.charid = (id)[obj objectForKey:@"toUser"];
wish.avatar = [[obj objectForKey:@"toUser"] objectForKey:@"avatar"];
wish.image = [[obj objectForKey:@"wish"] objectForKey:@"url"];
wish.age = [[obj objectForKey:@"toUser"] objectForKey:@"birthday"];
wish.context = [[obj objectForKey:@"wish"] objectForKey:@"text"];
wish.comment = [[obj objectForKey:@"wish"] objectForKey:@"comment"];
wish.like = [[obj objectForKey:@"wish"] objectForKey:@"like"];
BmobObject * timeobj = [obj objectForKey:@"wish"];
wish.attime = timeobj.updatedAt;
BmobGeoPoint * point = [[obj objectForKey:@"toUser"] objectForKey:@"location"];
CLLocationManager * locManager = [[CLLocationManager alloc] init];
locManager.desiredAccuracy = kCLLocationAccuracyBest;
[locManager startUpdatingLocation];
locManager.distanceFilter = 1000.0f;
//第一个坐标
CLLocation *current=[[CLLocation alloc] initWithLatitude:point.latitude longitude:point.longitude];
//第二个坐标
CLLocation *before=[[CLLocation alloc] initWithLatitude:locManager.location.coordinate.latitude longitude:locManager.location.coordinate.longitude];
// 计算距离
CLLocationDistance meters=[current distanceFromLocation:before];
wish.distance = [NSString stringWithFormat:@"%.2fkm",meters/1000];
[wishArray addObject:wish];
}
[helpTableView reloadData];
[_header endRefreshing];
[_footer endRefreshing];
}
}];
}
#pragma mark nabar
-(void)createNaBar{
UIView * gj = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 20)];
gj.backgroundColor = [UIColor blackColor];
[self.view addSubview:gj];
UIView * iv = [[UIView alloc] initWithFrame:CGRectMake(0, 20,320, self.view.bounds.size.height-20)];
iv.backgroundColor = [UIcol hexStringToColor:@"e6e6e6"];
[self.view addSubview:iv];
iv.userInteractionEnabled = YES;
UIImageView * bar = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
bar.image = [UIImage imageNamed:@"tou"];
[iv addSubview:bar];
UIButton * but = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 44,44)];
[but setImage:[UIImage imageNamed:@"fanhui"] forState:UIControlStateNormal];
[but setImage:[UIImage imageNamed:@"fanhuiselect"] forState:UIControlStateHighlighted];
[but addTarget:self action:@selector(goBack) forControlEvents:UIControlEventTouchUpInside];
[iv addSubview:but];
UILabel * titleBar = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
titleBar.text = @"评论";
titleBar.shadowColor = [UIcol hexStringToColor:@"cd9933"];
titleBar.shadowOffset = CGSizeMake(1, 0.5);
titleBar.font = [UIFont systemFontOfSize:18];
titleBar.textAlignment = YES;
titleBar.textColor = [UIcol hexStringToColor:@"ffffff"];
[iv addSubview:titleBar];
}
-(void)goBack{
[self.navigationController popViewControllerAnimated:YES];
}
- (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