forked from huobazi/DSBBM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyWishViewController.m
286 lines (234 loc) · 9.37 KB
/
MyWishViewController.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
//
// MyWishViewController.m
// DSBBM
//
// Created by bisheng on 14-9-19.
// Copyright (c) 2014年 qt. All rights reserved.
//
#import "MyWishViewController.h"
#import "WishTableViewCell.h"
#import "WishModel.h"
#import "DetailWishViewController.h"
#import "AddWishViewController.h"
@interface MyWishViewController ()
@end
@implementation MyWishViewController
{
UITableView * wishTableView;
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
wishArray = [NSMutableArray array];
_skip = 0;
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.navigationController.navigationBarHidden = YES;
[self createNaBar];
[self createTableView];
}
-(void)viewWillAppear:(BOOL)animated{
[self getDataSikp:_skip];
}
- (void)dealloc
{
[_header removeFromSuperview];
[_footer removeFromSuperview];
}
-(void)getDataSikp:(int)skip{
// BmobUser * user = [BmobUser getCurrentUser];
BmobQuery *wishBquery = [BmobQuery queryWithClassName:@"Wish"];
wishBquery.limit = 15;
wishBquery.skip = skip;
[wishBquery whereKey:@"user" equalTo:_userId];
[wishBquery includeKey:@"user"];
[wishBquery orderByDescending:@"createdAt"];
[wishBquery findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {
[HUD removeFromSuperview];
if(_skip == 0){
[wishArray removeAllObjects];
}
for (BmobObject *obj in array) {
BmobUser * postObj = [obj objectForKey:@"user"];
WishModel * wish = [[WishModel alloc] init];
wish.obj = obj;
wish.name = [postObj objectForKey:@"nick"];
wish.charid = (id)postObj;
wish.avatar = [postObj objectForKey:@"avatar"];
wish.image = [obj objectForKey:@"url"];
wish.age = [postObj objectForKey:@"birthday"];
wish.context = [obj objectForKey:@"text"];
wish.comment = [obj objectForKey:@"comment"];
wish.like = [obj objectForKey:@"like"];
wish.attime = obj.updatedAt;
[wishArray addObject:wish];
}
[wishTableView reloadData];
[_header endRefreshing];
[_footer endRefreshing];
}];
}
#pragma mark tableView
-(void)createTableView{
wishTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, 320, self.view.bounds.size.height-64)];
wishTableView.delegate = self;
wishTableView.dataSource = self;
wishTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
[self.view addSubview:wishTableView];
// 刷新功能
_header = [[MJRefreshHeaderView alloc] init];
_header.delegate = self;
_header.scrollView = wishTableView;
//添加上拉加载更多
_footer = [[MJRefreshFooterView alloc] init];
_footer.delegate = self;
_footer.scrollView = wishTableView;
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 getDataSikp:_skip];
}
- (void)reloadDeals
{
_skip = _skip + 15;
[self getDataSikp:_skip];
}
#pragma mark tableView 方法
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
WishTableViewCell * wishcell = [[WishTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"yw"];
if(!wishcell){
wishcell = [[[NSBundle mainBundle] loadNibNamed:@"yw" owner:self options:nil] lastObject];
}
WishModel * wish = wishArray[indexPath.row];
NSURL * avatarimu = [NSURL URLWithString:wish.avatar];
[wishcell.avatar sd_setImageWithURL:avatarimu];
NSURL * imageimu = [NSURL URLWithString:wish.image];
[wishcell.image sd_setImageWithURL:imageimu];
wishcell.name.text = wish.name;
NSDateFormatter *date=[[NSDateFormatter alloc] init];
[date setDateFormat:@"yyyy-MM-dd"];
NSDate *d=[date dateFromString:wish.age];
NSTimeInterval dateDiff = [d timeIntervalSinceNow];
int age=trunc(dateDiff/(60*60*24))/365;
wishcell.age.text = [NSString stringWithFormat:@"%d",-age];
wishcell.context.text = wish.context;
wishcell.like.text = [NSString stringWithFormat:@"%d",[wish.like intValue]];
wishcell.comment.text = [NSString stringWithFormat:@"%d",[wish.comment intValue]];
wishcell.distance.text = [NSString stringWithFormat:@"%@",[mistiming returnUploadTime:[NSString stringWithFormat:@"%@",wish.attime]]];
return wishcell;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return wishArray.count;}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 115;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
DetailWishViewController * detailwish = [[DetailWishViewController alloc] init];
WishModel * wish = wishArray[indexPath.row];
detailwish.number = 1;
detailwish.objectId = wish.obj.objectId;
[self.navigationController pushViewController:detailwish animated:YES];
}
#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];
BmobUser * user = [BmobUser getCurrentObject];
UILabel * titleBar = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
if([_userId isEqualToString:user.objectId]){
titleBar.text = @"我的愿望";
}else{
titleBar.text = self.userName;
}
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];
if([self.userName isEqualToString:@""]){
UIButton * publishBtn = [[UIButton alloc] initWithFrame:CGRectMake(266, 0, 54, 44)];
[publishBtn setImage:[UIcol imageWithColor:[UIColor clearColor] size:CGSizeMake(70, 44)] forState:UIControlStateNormal];
[publishBtn setImage:[UIcol imageWithColor:[UIColor colorWithRed:0.9 green:0.74 blue:0 alpha:1] size:CGSizeMake(70, 44)] forState:UIControlStateHighlighted];
[publishBtn addTarget:self action:@selector(addWish) forControlEvents:UIControlEventTouchUpInside];
UILabel * publishLab = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 44, 44)];
publishLab.text = @"新增";
publishLab.shadowColor = [UIcol hexStringToColor:@"cd9933"];
publishLab.shadowOffset = CGSizeMake(1, 0.5);
publishLab.textColor = [UIColor whiteColor];
[publishBtn addSubview:publishLab];
[iv addSubview:publishBtn];
}
}
-(void)addWish{
AddWishViewController * awvc = [[AddWishViewController alloc] init];
awvc.k = 2;
[self.navigationController pushViewController:awvc animated:YES];
}
-(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