-
Notifications
You must be signed in to change notification settings - Fork 3
/
MyListViewController.m
executable file
·312 lines (247 loc) · 10.1 KB
/
MyListViewController.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
//
// MyListViewController.m
// IWords
//
// Created by yaonphy on 12-11-16.
// Copyright (c) 2012年 yang. All rights reserved.
//
#import "MyListViewController.h"
@interface MyListViewController ()
//界面基本参数
- (void) setNavBar;
- (void) backAction;
- (void) addBasicView;
//左右滑动相关
- (void)initScrollView;
- (void)scrollViewDidScroll:(UIScrollView *)scrollView;
- (void)createAllEmptyPagesForScrollView;
//界面按钮事件
- (void) btnActionShow;
- (void) couponButtonAction;
- (void) groupbuyButtonAction;
@end
@implementation MyListViewController
@synthesize couponButton;
@synthesize groupbuyButton;
@synthesize slidLabel;
@synthesize nibScrollView;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
// [super viewDidLoad];
// Do any additional setup after loading the view.
[self initNavItem];
[self setNavBar];
[self addBasicView];
[self initScrollView];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (void)dealloc {
[couponButton release];
[groupbuyButton release];
[slidLabel release];
[nibScrollView release];
[super dealloc];
}
#pragma mark-
#pragma mark navigationController Methods
- (void) setNavBar
{
// self.navigationController.navigationBarHidden = NO;//显示nav,这里使用动画
self.navigationItem.title = @"iWordsList";
//设置Navigation Bar颜色
//
// UIButton *BackBtn = [[UIButton alloc] initWithFrame:CGRectMake(0.0, 164.0, 45, 45)];
// [BackBtn setImage:[UIImage imageNamed:@"btn_back1.png"] forState:UIControlStateNormal];
// [BackBtn setImage:[UIImage imageNamed:@"btn_back2"] forState:UIControlStateHighlighted];
// BackBtn.backgroundColor = [UIColor clearColor];
// [BackBtn addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
// UIBarButtonItem *temporaryBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:BackBtn];
// temporaryBarButtonItem.style = UIBarButtonItemStylePlain;
// self.navigationItem.leftBarButtonItem = temporaryBarButtonItem;
// [temporaryBarButtonItem release];
}
- (void) backAction
{
[self.navigationController popViewControllerAnimated:YES];
}
- (void) addBasicView
{
couponButton.showsTouchWhenHighlighted = YES; //指定按钮被按下时发光
[couponButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];//此时选中
[couponButton addTarget:self action:@selector(couponButtonAction) forControlEvents:UIControlEventTouchUpInside];
groupbuyButton.showsTouchWhenHighlighted = YES; //指定按钮被按下时发光
[groupbuyButton setTitleColor:[UIColor colorWithRed:(220/255.0) green:(220/255.0) blue:(220/255.0) alpha:1] forState:UIControlStateNormal];//此时未被选中
[groupbuyButton addTarget:self action:@selector(groupbuyButtonAction) forControlEvents:UIControlEventTouchUpInside];
}
#pragma mark-
#pragma mark 界面按钮事件
- (void) btnActionShow
{
if (currentPage == 0) {
[self couponButtonAction];
}
else{
[self groupbuyButtonAction];
}
}
- (void) couponButtonAction
{
[couponButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];//此时选中
[groupbuyButton setTitleColor:[UIColor colorWithRed:(220/255.0) green:(220/255.0) blue:(220/255.0) alpha:1] forState:UIControlStateNormal];//此时未被选中
[UIView beginAnimations:nil context:nil];//动画开始
[UIView setAnimationDuration:0.3];
slidLabel.frame = CGRectMake(0, 36, 160, 4);
[nibScrollView setContentOffset:CGPointMake(320*0, 0)];//页面滑动
[UIView commitAnimations];
}
- (void) groupbuyButtonAction
{
[groupbuyButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];//此时选中
[couponButton setTitleColor:[UIColor colorWithRed:(220/255.0) green:(220/255.0) blue:(220/255.0) alpha:1] forState:UIControlStateNormal];//此时未被选中
[UIView beginAnimations:nil context:nil];//动画开始
[UIView setAnimationDuration:0.3];
slidLabel.frame = CGRectMake(159, 36, 161, 4);
[nibScrollView setContentOffset:CGPointMake(320*1, 0)];
[UIView commitAnimations];
}
//表视图委托
#pragma mark -
#pragma mark table view data source methods
//返回某个表视图有多少行数据
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView == couponTableView) {
return couponArry.count;
}
else{
return groupbuyArry.count;
}
}
//表视图显示表视图项时调用:第一次显示(根据视图大小显示多少个视图项就调用多少次)以及拖动时调用
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier: CellIdentifier] autorelease];
}else{
//cell中本来就有一个subview,如果是重用cell,则把cell中自己添加的subview清除掉,避免出现重叠问题
// [[cell.subviews objectAtIndex:1] removeFromSuperview];
for (UIView *subView in cell.contentView.subviews)
{
[subView removeFromSuperview];
}
}
if (tableView == couponTableView) {
//进入优惠券列表
cell.textLabel.text = [NSString stringWithFormat:@"%@", [couponArry objectAtIndex:indexPath.row]];
}
else{
//进入团购列表
cell.textLabel.text = [NSString stringWithFormat:@"%@", [groupbuyArry objectAtIndex:indexPath.row]];
}
return cell;
}
//数据源委托
#pragma mark -
#pragma mark table delegate methods
//在某行被选中前调用,返回nil表示该行不能被选中;另外也可返回重定向的indexPath,使选择某行时会跳到另一行
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath{
return indexPath;
}
//某行已经被选中时调用
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[tableView deselectRowAtIndexPath:indexPath animated:YES];//在弹出警告后自动取消选中表视图单元
if (tableView == couponTableView) {
//进入优惠券详情 -- 估计需要再添加一个判断语句用来不请求列表操作
}
else{
//进入团购详情 -- 估计需要再添加一个判断语句用来不请求列表操作
}
}
//设置每行缩进级别
- (NSInteger) tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 0;
}
//设置行高度
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 80;
}
#pragma mark -
#pragma mark 左右滑动相关函数
- (void)initScrollView {
//设置 tableScrollView
// a page is the width of the scroll view
nibScrollView.pagingEnabled = YES;
nibScrollView.clipsToBounds = NO;
nibScrollView.contentSize = CGSizeMake(nibScrollView.frame.size.width * 2, nibScrollView.frame.size.height);
nibScrollView.showsHorizontalScrollIndicator = NO;
nibScrollView.showsVerticalScrollIndicator = NO;
nibScrollView.scrollsToTop = NO;
nibScrollView.delegate = self;
[nibScrollView setContentOffset:CGPointMake(0, 0)];
//公用
currentPage = 0;
pageControl.numberOfPages = 2;
pageControl.currentPage = 0;
pageControl.backgroundColor = [UIColor whiteColor];
[self createAllEmptyPagesForScrollView];
}
- (void)createAllEmptyPagesForScrollView {
//设置 tableScrollView 内部数据
couponTableView = [[UITableView alloc]init ];
couponTableView.frame = CGRectMake(320*0, 0, 320, nibScrollView.frame.size.height);
groupbuyTableView = [[UITableView alloc]init ];
groupbuyTableView.frame = CGRectMake(320*1, 0, 320, nibScrollView.frame.size.height);
//设置tableView委托并添加进视图
couponTableView.delegate = self;
couponTableView.dataSource = self;
[nibScrollView addSubview: couponTableView];
groupbuyTableView.delegate = self;
groupbuyTableView.dataSource = self;
[nibScrollView addSubview: groupbuyTableView];
//设置 nibTableView 数据源数组 -- 仅仅用与测试
couponArry = [[NSArray alloc]initWithObjects:@"coupon1",@"coupon2",@"coupon3", @"coupon4",nil ];
groupbuyArry = [[NSArray alloc]initWithObjects:@"groupbuy1",@"groupbuy2",@"groupbuy3", nil ];
}
// At the end of scroll animation, reset the boolean used when scrolls originate from the UIPageControl
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
CGFloat pageWidth = self.nibScrollView.frame.size.width;
int page = floor((self.nibScrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
pageControl.currentPage = page;
currentPage = page;
pageControlUsed = NO;
[self btnActionShow];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
//暂不处理 - 其实左右滑动还有包含开始等等操作,这里不做介绍
}
- (BOOL)touchesShouldBegin:(NSSet *)touches withEvent:(UIEvent *)event inContentView:(UIView *)view
{
return YES;
}
- (BOOL)touchesShouldCancelInContentView:(UIView *)view
{
return NO;
}
@end