实现多列表切换,位置监控,置顶等功能。

方法一:

创建一个TableView,在切换的时候请求各类目需要的数据,然后刷新列表,通过动画模拟出滑动效果。

  1. #import <UIKit/UIKit.h>
  2.  
  3. @interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate,UIScrollViewDelegate>
  4. {
  5. //选择视图数组
  6. NSMutableArray *selectArray;
  7. //选择视图
  8. UIView *selectView;
  9. //列表
  10. UITableView *jiuJiuTableView;
  11. //选择类型
  12. NSInteger priceType;
  13. //切换之前的类型
  14. NSInteger oldType;
  15. //数量显示(返回顶部)
  16. UIView *countView;
  17. //记录tableview的contentOffset.y
  18. CGFloat lastY;
  19. //返回顶部
  20. UIButton *toTopButton;
  21. //记录tableview的位置
  22. NSMutableDictionary *jiLuDictionary;
  23. }
  24. //数据源
  25. @property (nonatomic, strong) NSMutableArray *tableArray;
  26.  
  27. @end
  1. #import "ViewController.h"
  2. //tableview的cell的高度
  3. #define cellHeight self.view.frame.size.width*0.4
  4. @interface ViewController ()
  5.  
  6. @end
  7.  
  8. @implementation ViewController
  9. @synthesize tableArray;
  10. - (void)viewDidLoad {
  11. [super viewDidLoad];
  12. self.view.backgroundColor = [UIColor whiteColor];
  13.  
  14. //初始化数据源
  15. [self initTableArray];
  16. //初始化分类数据
  17. [self initSelectArray];
  18. //添加选择视图
  19. [self addSelectView];
  20. //添加滚动列表
  21. [self addTableView];
  22. }
  23. //初始化数据源
  24. - (void)initTableArray
  25. {
  26. //数据源
  27. if(self.tableArray == nil)
  28. {
  29. //初始化(实际使用时在此处请求类型1的数据即可)
  30. self.tableArray = [[NSMutableArray alloc] initWithObjects:@"类型1",@"类型1",@"类型1",@"类型1",@"类型1",@"类型1",@"类型1",@"类型1",@"类型1",@"类型1",@"类型1", nil];
  31. jiLuDictionary = [[NSMutableDictionary alloc] init];
  32. }
  33. }
  34.  
  35. //初始化分类数据
  36. - (void)initSelectArray
  37. {
  38. priceType = ;
  39. oldType = ;
  40. if (selectArray == nil)
  41. {
  42. selectArray = [[NSMutableArray alloc] initWithObjects:@"类型1",@"类型2",@"类型3", nil];
  43. }
  44. }
  45. //添加选择视图
  46. - (void)addSelectView
  47. {
  48. selectView = [[UIView alloc] initWithFrame:CGRectMake(, , self.view.frame.size.width, )];
  49. [self.view addSubview:selectView];
  50.  
  51. CGFloat buttonW = self.view.frame.size.width/selectArray.count;
  52. for (int i = ; i < selectArray.count; i++ )
  53. {
  54. UIButton *selectButton = [[UIButton alloc] initWithFrame:CGRectMake(buttonW*i, , buttonW, )];
  55. selectButton.tag = +i;
  56. [selectButton setTitle:selectArray[i] forState:UIControlStateNormal];
  57. [selectButton setTitleColor:[UIColor colorWithRed:69.0/255.0 green:69.0/255.0 blue:69.0/255.0 alpha:1.0] forState:UIControlStateNormal];
  58. [selectButton.titleLabel setFont:[UIFont systemFontOfSize:]];
  59. [selectButton addTarget:self action:@selector(clickedSelectButton:) forControlEvents:UIControlEventTouchUpInside];
  60. [selectView addSubview:selectButton];
  61. if (i != )
  62. {
  63. //分隔线
  64. UIImageView *lineImageView = [[UIImageView alloc] initWithFrame:CGRectMake(buttonW*i, (-)/, , )];
  65. lineImageView.backgroundColor = [UIColor colorWithRed:238.0/255.0 green:238.0/255.0 blue:238.0/255.0 alpha:1.0];
  66. [selectView addSubview:lineImageView];
  67. }
  68. else
  69. {
  70. [selectButton setTitleColor:[UIColor colorWithRed:255.0/255.0 green:83.0/255.0 blue:96.0/255.0 alpha:1.0] forState:UIControlStateNormal];
  71. [selectButton.titleLabel setFont:[UIFont systemFontOfSize:]];
  72. }
  73. }
  74. UIImageView *lineImageView = [[UIImageView alloc] initWithFrame:CGRectMake(, -, self.view.frame.size.width, )];
  75. lineImageView.backgroundColor = [UIColor colorWithRed:222.0/255.0 green:222.0/255.0 blue:222.0/255.0 alpha:1.0];
  76. [selectView addSubview:lineImageView];
  77. }
  78. //添加列表
  79. - (void)addTableView
  80. {
  81. if (jiuJiuTableView == nil)
  82. {
  83. jiuJiuTableView = [[UITableView alloc] initWithFrame:CGRectMake(, +, self.view.frame.size.width, self.view.frame.size.height--) style:UITableViewStylePlain];
  84. jiuJiuTableView.dataSource = self;
  85. jiuJiuTableView.delegate = self;
  86. //默认分隔线设置为无
  87. jiuJiuTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  88. [self.view addSubview:jiuJiuTableView];
  89.  
  90. //数量显示(返回顶部)
  91. countView = [[UIView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(jiuJiuTableView.frame)-, CGRectGetMaxY(jiuJiuTableView.frame)-, , )];
  92. countView.backgroundColor = [UIColor colorWithRed:/255.0 green:/255.0 blue:/255.0 alpha:0.8];
  93. countView.layer.masksToBounds = YES;
  94. countView.layer.cornerRadius = ;
  95. [self.view addSubview:countView];
  96. UIImageView *lineView = [[UIImageView alloc] initWithFrame:CGRectMake((-)/, (-)/, , )];
  97. lineView.backgroundColor = [UIColor whiteColor];
  98. lineView.alpha = 0.8;
  99. [countView addSubview:lineView];
  100. UILabel *currentCount = [[UILabel alloc] initWithFrame:CGRectMake(lineView.frame.origin.x, lineView.frame.origin.y-, lineView.frame.size.width, )];
  101. currentCount.tag = ;
  102. currentCount.textAlignment = NSTextAlignmentCenter;
  103. currentCount.textColor = [UIColor whiteColor];
  104. currentCount.font = [UIFont systemFontOfSize:];
  105. [countView addSubview:currentCount];
  106. UILabel *totalCount = [[UILabel alloc] initWithFrame:CGRectMake(lineView.frame.origin.x, lineView.frame.origin.y+, lineView.frame.size.width, )];
  107. totalCount.tag = ;
  108. totalCount.textAlignment = NSTextAlignmentCenter;
  109. totalCount.textColor = [UIColor whiteColor];
  110. totalCount.font = [UIFont systemFontOfSize:];
  111. [countView addSubview:totalCount];
  112. countView.hidden = YES;
  113. //返回顶部
  114. toTopButton = [[UIButton alloc] initWithFrame:countView.frame];
  115. toTopButton.layer.masksToBounds = YES;
  116. toTopButton.layer.cornerRadius = ;
  117. [toTopButton setImage:[UIImage imageNamed:@"clickedToTop.png"] forState:UIControlStateNormal];
  118. toTopButton.alpha = 0.8;
  119. [toTopButton addTarget:self action:@selector(clickedToTop) forControlEvents:UIControlEventTouchUpInside];
  120. [self.view addSubview:toTopButton];
  121. toTopButton.hidden = YES;
  122. //添加滑动手势
  123. UISwipeGestureRecognizer *swipeGestureToRight=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeToRight)];
  124. swipeGestureToRight.direction=UISwipeGestureRecognizerDirectionRight;
  125. [jiuJiuTableView addGestureRecognizer:swipeGestureToRight];
  126.  
  127. UISwipeGestureRecognizer *swipeGestureToLeft=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeToLeft)];
  128. swipeGestureToLeft.direction=UISwipeGestureRecognizerDirectionLeft;
  129. [jiuJiuTableView addGestureRecognizer:swipeGestureToLeft];
  130. }
  131. }
  132. #pragma mark ------------------点击事件----------------------
  133. //记录各类别tableview的位置
  134. - (void)writeJiLuDictionary
  135. {
  136. NSNumber *jiLuNum = [NSNumber numberWithFloat:jiuJiuTableView.contentOffset.y];
  137. if (priceType == )
  138. {
  139. [jiLuDictionary setObject:jiLuNum forKey:@"jiuJiuY"];
  140. }
  141. else if (priceType == )
  142. {
  143. [jiLuDictionary setObject:jiLuNum forKey:@"shiJiuY"];
  144. }
  145. else if (priceType == )
  146. {
  147. [jiLuDictionary setObject:jiLuNum forKey:@"erJiuY"];
  148. }
  149. }
  150. //点击选择按钮事件
  151. - (void)clickedSelectButton:(UIButton *)sender
  152. {
  153. UIButton *button = sender;
  154. [self writeJiLuDictionary];
  155. if (button.tag == )
  156. {
  157. priceType = ;
  158. }
  159. else if (button.tag == )
  160. {
  161. priceType = ;
  162. }
  163. else if (button.tag == )
  164. {
  165. priceType = ;
  166. }
  167. [self changeUIAndReloadData];
  168. }
  169. //左右滑动事件
  170. - (void)swipeToRight
  171. {
  172. [self writeJiLuDictionary];
  173. if (priceType == )
  174. {
  175. return;
  176. }
  177. else
  178. {
  179. priceType = priceType-;
  180. }
  181. [self changeUIAndReloadData];
  182. }
  183. - (void)swipeToLeft
  184. {
  185. [self writeJiLuDictionary];
  186. if (priceType == )
  187. {
  188. return;
  189. }
  190. else
  191. {
  192. priceType = priceType+;
  193. }
  194. [self changeUIAndReloadData];
  195. }
  196. //改变UI,刷新数据
  197. - (void)changeUIAndReloadData
  198. {
  199. NSInteger buttonTag = ;
  200. if (priceType == )
  201. {
  202. buttonTag = ;
  203. [jiuJiuTableView setContentOffset:CGPointMake(, [jiLuDictionary[@"jiuJiuY"] floatValue]) animated:NO];
  204. //实际使用时将此处换成请求语句,在请求成功的方法中得到请求数据之后更新数组,刷新列表即可。例如在方法“requestJiuYuanNewFinish”中。
  205. [self requestJiuYuanNewFinish:[NSArray arrayWithObjects:@"类型1",@"类型1",@"类型1",@"类型1",@"类型1",@"类型1",@"类型1",@"类型1",@"类型1",@"类型1",@"类型1", nil]];
  206. }
  207. else if (priceType == )
  208. {
  209. buttonTag = ;
  210. [jiuJiuTableView setContentOffset:CGPointMake(, [jiLuDictionary[@"shiJiuY"] floatValue]) animated:NO];
  211. //实际使用时将此处换成请求语句,在请求成功的方法中得到请求数据之后更新数组,刷新列表即可。例如在方法“requestJiuYuanNewFinish”中。
  212. [self requestJiuYuanNewFinish:[NSArray arrayWithObjects:@"类型2",@"类型2",@"类型2",@"类型2",@"类型2",@"类型2",@"类型2",@"类型2",@"类型2",@"类型2",@"类型2", nil]];
  213. }
  214. else if (priceType == )
  215. {
  216. buttonTag = ;
  217. [jiuJiuTableView setContentOffset:CGPointMake(, [jiLuDictionary[@"erJiuY"] floatValue]) animated:NO];
  218. //实际使用时将此处换成请求语句,在请求成功的方法中得到请求数据之后更新数组,刷新列表即可。例如在方法“requestJiuYuanNewFinish”中。
  219. [self requestJiuYuanNewFinish:[NSArray arrayWithObjects:@"类型3",@"类型3",@"类型3",@"类型3",@"类型3",@"类型3",@"类型3",@"类型3",@"类型3",@"类型3",@"类型3", nil]];
  220. }
  221. for (int j = ; j < +selectArray.count; j++)
  222. {
  223. UIButton *selectButton = (UIButton *)[selectView viewWithTag:j];
  224. if (j == buttonTag)
  225. {
  226. [selectButton setTitleColor:[UIColor colorWithRed:255.0/255.0 green:83.0/255.0 blue:96.0/255.0 alpha:1.0] forState:UIControlStateNormal];
  227. [selectButton.titleLabel setFont:[UIFont systemFontOfSize:]];
  228. }
  229. else
  230. {
  231. [selectButton setTitleColor:[UIColor colorWithRed:69.0/255.0 green:69.0/255.0 blue:69.0/255.0 alpha:1.0] forState:UIControlStateNormal];
  232. [selectButton.titleLabel setFont:[UIFont systemFontOfSize:]];
  233. }
  234. }
  235. //添加动画
  236. CATransition *transition = [CATransition animation];
  237. transition.delegate = self;
  238. transition.duration = 0.5f;
  239. transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
  240. transition.type = kCATransitionPush;
  241. if (oldType == priceType)
  242. {
  243. return;
  244. }
  245. else if (oldType < priceType)
  246. {
  247. transition.subtype = kCATransitionFromRight;
  248. }
  249. else
  250. {
  251. transition.subtype = kCATransitionFromLeft;
  252. }
  253. oldType = priceType;
  254. [jiuJiuTableView reloadData];
  255.  
  256. [jiuJiuTableView.layer addAnimation:transition forKey:@"animation"];
  257. }
  258. //返回顶部事件
  259. - (void)clickedToTop
  260. {
  261. [jiuJiuTableView setContentOffset:CGPointMake(, ) animated:YES];
  262. }
  263. #pragma mark --------------tableviewDataSource---------------
  264. //每组的行数
  265. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  266. {
  267. return self.tableArray.count;
  268. }
  269. //设置行数据
  270. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  271. {
  272. static NSString *cellString = @"cellString";//cell的重用
  273. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellString];
  274. if (cell == nil)
  275. {
  276. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellString];
  277. //设置点选效果为无
  278. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  279. cell.accessoryType = UITableViewCellAccessoryNone;
  280. }
  281. cell.textLabel.text = [NSString stringWithFormat:@"%@第%li行",self.tableArray[indexPath.row],(long)indexPath.row+];
  282. //更新数量
  283. UILabel *currentCount = (UILabel *)[countView viewWithTag:];
  284. UILabel *totalCount = (UILabel *)[countView viewWithTag:];
  285. currentCount.text = [NSString stringWithFormat:@"%li",(long)indexPath.row+];
  286. totalCount.text = [NSString stringWithFormat:@"%li",(long)self.tableArray.count];
  287. //分隔线
  288. UIImageView *lineImageView = [[UIImageView alloc] initWithFrame:CGRectMake(, cellHeight-, self.view.frame.size.width-, )];
  289. [lineImageView setBackgroundColor:[UIColor colorWithRed:234.0/255.0 green:234.0/255.0 blue:234.0/255.0 alpha:1.0]];
  290. [cell addSubview:lineImageView];
  291.  
  292. return cell;
  293. }
  294. #pragma mark ---------------tableviewDelegate------------------
  295. //设置行高
  296. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  297. {
  298. return cellHeight;
  299. }
  300.  
  301. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  302. {
  303. NSLog(@"点击了第%ld行",indexPath.row+);
  304. }
  305.  
  306. #pragma mark ----------------UIScrollViewDelegate-----------------
  307. - (void)scrollViewDidScroll:(UIScrollView *)scrollView
  308. {
  309. if (scrollView == jiuJiuTableView)
  310. {
  311. CGFloat y = scrollView.contentOffset.y;
  312. if (y < cellHeight*)
  313. {
  314. countView.hidden = YES;
  315. toTopButton.hidden = YES;
  316. }
  317. else
  318. {
  319. if (y > lastY)
  320. {
  321. countView.hidden = NO;
  322. toTopButton.hidden = YES;
  323. }
  324. else
  325. {
  326. countView.hidden = YES;
  327. toTopButton.hidden = NO;
  328. }
  329. }
  330. lastY = y;
  331. }
  332. }
  333. //请求最新数据成功
  334. -(void)requestJiuYuanNewFinish:(NSArray *)array
  335. {
  336. [self.tableArray removeAllObjects];
  337. [self.tableArray addObjectsFromArray:array];
  338. //刷新列表
  339. //[jiuJiuTableView reloadData];
  340. }

方法二:

创建多个TableView,放在ScrollView中,实现横向滑动。(相比性能等方面,推荐使用第一种方法)

  1. #import <UIKit/UIKit.h>
  2. @interface JiuKuaiJiuViewController : UIViewController<UITableViewDataSource,UITableViewDelegate,UIScrollViewDelegate>
  3. {
  4. //两列的collectionview
  5. UICollectionView *JFCollectionView;
  6. //选择视图数组
  7. NSArray *selectArray;
  8. //列表容器
  9. UIScrollView *containerScrollView;
  10. }
  11.  
  12. //数据源
  13. @property(nonatomic, strong) NSMutableArray *tableArray;
  14.  
  15. @end
  1. #import "JiuKuaiJiuViewController.h"
  2.  
  3. //tableview的cell的高度
  4. #define cellHeight self.view.frame.size.width*0.4
  5.  
  6. @interface JiuKuaiJiuViewController ()
  7. @end
  8.  
  9. @implementation JiuKuaiJiuViewController
  10. @synthesize tableArray;
  11. - (void)viewDidLoad
  12. {
  13. [super viewDidLoad];
  14. self.view.backgroundColor = [UIColor whiteColor];
  15. //初始化数据源
  16. [self initTableArray];
  17. //初始化分类数据
  18. [self initSelectArray];
  19. //添加选择视图
  20. [self addSelectView];
  21. //添加滚动列表
  22. [self addTableView];
  23. }
  24. //初始化数据源
  25. - (void)initTableArray
  26. {
  27. if (self.tableArray == nil)
  28. {
  29. self.tableArray = [[NSMutableArray alloc] initWithObjects:@"类型1",@"类型1",@"类型1",@"类型1",@"类型1",@"类型1",@"类型1",@"类型1",@"类型1",@"类型1",@"类型1", nil];
  30. }
  31. }
  32.  
  33. //初始化分类数据
  34. - (void)initSelectArray
  35. {
  36. if (selectArray == nil)
  37. {
  38. selectArray = [[NSArray alloc] initWithObjects:@"类型1",@"类型2",@"类型3", nil];
  39. }
  40. }
  41. //添加选择视图
  42. - (void)addSelectView
  43. {
  44. CGFloat buttonW = self.view.frame.size.width/selectArray.count;
  45. for (int i = ; i < selectArray.count; i++ )
  46. {
  47. UIButton *selectButton = [[UIButton alloc] initWithFrame:CGRectMake(buttonW*i, , buttonW, )];
  48. selectButton.tag = +i;
  49. [selectButton setTitle:selectArray[i] forState:UIControlStateNormal];
  50. [selectButton setTitleColor:[UIColor colorWithRed:69.0/255.0 green:69.0/255.0 blue:69.0/255.0 alpha:1.0] forState:UIControlStateNormal];
  51. [selectButton.titleLabel setFont:[UIFont systemFontOfSize:]];
  52. [selectButton addTarget:self action:@selector(clickedSelectButton:) forControlEvents:UIControlEventTouchUpInside];
  53. [self.view addSubview:selectButton];
  54. if (i != )
  55. {
  56. //分隔线
  57. UIImageView *lineImageView = [[UIImageView alloc] initWithFrame:CGRectMake(buttonW*i, +(-)/, , )];
  58. lineImageView.backgroundColor = [UIColor colorWithRed:238.0/255.0 green:238.0/255.0 blue:238.0/255.0 alpha:1.0];
  59. [self.view addSubview:lineImageView];
  60. }
  61. else
  62. {
  63. [selectButton setTitleColor:[UIColor colorWithRed:255.0/255.0 green:83.0/255.0 blue:96.0/255.0 alpha:1.0] forState:UIControlStateNormal];
  64. [selectButton.titleLabel setFont:[UIFont systemFontOfSize:]];
  65. }
  66. }
  67. UIImageView *lineImageView = [[UIImageView alloc] initWithFrame:CGRectMake(, +, self.view.frame.size.width, )];
  68. lineImageView.backgroundColor = [UIColor colorWithRed:222.0/255.0 green:222.0/255.0 blue:222.0/255.0 alpha:1.0];
  69. [self.view addSubview:lineImageView];
  70. }
  71. //添加列表
  72. - (void)addTableView
  73. {
  74. if (containerScrollView == nil)
  75. {
  76. containerScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(, ++, self.view.frame.size.width, self.view.frame.size.height---)];
  77. containerScrollView.delegate = self;
  78. containerScrollView.pagingEnabled = YES;
  79. containerScrollView.contentSize = CGSizeMake(self.view.frame.size.width * selectArray.count, containerScrollView.frame.size.height);
  80. [self.view addSubview:containerScrollView];
  81.  
  82. for (int k = ; k < selectArray.count; k++)
  83. {
  84. UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(self.view.frame.size.width*k, , self.view.frame.size.width, containerScrollView.frame.size.height) style:UITableViewStylePlain];
  85. tableView.tag = +k;
  86. tableView.dataSource = self;
  87. tableView.delegate = self;
  88. //默认分隔线设置为无
  89. tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  90. [containerScrollView addSubview:tableView];
  91. }
  92. }
  93. else
  94. {
  95. //刷新列表
  96. for (int i = ; i < +selectArray.count; i++)
  97. {
  98. UITableView *tableView = (UITableView *)[containerScrollView viewWithTag:i];
  99. [tableView reloadData];
  100. }
  101. }
  102. }
  103.  
  104. //点击选择按钮事件
  105. - (void)clickedSelectButton:(UIButton *)sender
  106. {
  107. UIButton *button = sender;
  108. for (int j = ; j < +selectArray.count; j++)
  109. {
  110. UIButton *selectButton = (UIButton *)[self.view viewWithTag:j];
  111. if (j == button.tag)
  112. {
  113. [selectButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
  114. [selectButton.titleLabel setFont:[UIFont systemFontOfSize:]];
  115. }
  116. else
  117. {
  118. [selectButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
  119. [selectButton.titleLabel setFont:[UIFont systemFontOfSize:]];
  120. }
  121. }
  122. if (button.tag == )
  123. {
  124. [containerScrollView setContentOffset:CGPointMake(, ) animated:YES];
  125. //实际使用时将此处换成请求语句,在请求成功的方法中得到请求数据之后更新数组,刷新列表即可。例如在方法“requestJiuYuanNewFinish”中。
  126. [self requestJiuYuanNewFinish:[NSArray arrayWithObjects:@"类型1",@"类型1",@"类型1",@"类型1",@"类型1",@"类型1",@"类型1",@"类型1",@"类型1",@"类型1",@"类型1", nil]];
  127. }
  128. else if (button.tag == )
  129. {
  130. [containerScrollView setContentOffset:CGPointMake(self.view.frame.size.width, ) animated:YES];
  131. //实际使用时将此处换成请求语句,在请求成功的方法中得到请求数据之后更新数组,刷新列表即可。例如在方法“requestJiuYuanNewFinish”中。
  132. [self requestJiuYuanNewFinish:[NSArray arrayWithObjects:@"类型2",@"类型2",@"类型2",@"类型2",@"类型2",@"类型2",@"类型2",@"类型2",@"类型2",@"类型2",@"类型2", nil]];
  133. }
  134. else if (button.tag == )
  135. {
  136. [containerScrollView setContentOffset:CGPointMake(self.view.frame.size.width*, ) animated:YES];
  137. //实际使用时将此处换成请求语句,在请求成功的方法中得到请求数据之后更新数组,刷新列表即可。例如在方法“requestJiuYuanNewFinish”中。
  138. [self requestJiuYuanNewFinish:[NSArray arrayWithObjects:@"类型3",@"类型3",@"类型3",@"类型3",@"类型3",@"类型3",@"类型3",@"类型3",@"类型3",@"类型3",@"类型3", nil]];
  139. }
  140. }
  141.  
  142. #pragma mark --------------tableviewDataSource---------------
  143. //每组的行数
  144. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  145. {
  146. return self.tableArray.count;
  147. }
  148. //设置行数据
  149. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  150. {
  151. static NSString *cellString = @"cellString";//cell的重用
  152. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellString];
  153. if (cell == nil)
  154. {
  155. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellString];
  156. //设置点选效果为无
  157. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  158. cell.accessoryType = UITableViewCellAccessoryNone;
  159. }
  160. cell.textLabel.text = [NSString stringWithFormat:@"%@第%li行",self.tableArray[indexPath.row],(long)indexPath.row+];
  161. //分隔线
  162. UIImageView *lineImageView = [[UIImageView alloc] initWithFrame:CGRectMake(, cellHeight-, self.view.frame.size.width-, )];
  163. [lineImageView setBackgroundColor:[UIColor colorWithRed:234.0/255.0 green:234.0/255.0 blue:234.0/255.0 alpha:1.0]];
  164. [cell addSubview:lineImageView];
  165.  
  166. return cell;
  167. }
  168. #pragma mark ---------------tableviewDelegate------------------
  169. //设置行高
  170. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  171. {
  172. return cellHeight;
  173. }
  174. #pragma mark ----------------scrollViewDelegate----------------
  175. - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
  176. {
  177. if (scrollView == containerScrollView)
  178. {
  179. CGPoint point = scrollView.contentOffset;
  180. NSInteger selectIndex = ;
  181. if (point.x == )
  182. {
  183. selectIndex = ;
  184. //实际使用时将此处换成请求语句,在请求成功的方法中得到请求数据之后更新数组,刷新列表即可。例如在方法“requestJiuYuanNewFinish”中。
  185. [self requestJiuYuanNewFinish:[NSArray arrayWithObjects:@"类型1",@"类型1",@"类型1",@"类型1",@"类型1",@"类型1",@"类型1",@"类型1",@"类型1",@"类型1",@"类型1", nil]];
  186. }
  187. else if (point.x == self.view.frame.size.width)
  188. {
  189. selectIndex = ;
  190. //实际使用时将此处换成请求语句,在请求成功的方法中得到请求数据之后更新数组,刷新列表即可。例如在方法“requestJiuYuanNewFinish”中。
  191. [self requestJiuYuanNewFinish:[NSArray arrayWithObjects:@"类型2",@"类型2",@"类型2",@"类型2",@"类型2",@"类型2",@"类型2",@"类型2",@"类型2",@"类型2",@"类型2", nil]];
  192. }
  193. else if (point.x == self.view.frame.size.width*)
  194. {
  195. selectIndex = ;
  196. //实际使用时将此处换成请求语句,在请求成功的方法中得到请求数据之后更新数组,刷新列表即可。例如在方法“requestJiuYuanNewFinish”中。
  197. [self requestJiuYuanNewFinish:[NSArray arrayWithObjects:@"类型3",@"类型3",@"类型3",@"类型3",@"类型3",@"类型3",@"类型3",@"类型3",@"类型3",@"类型3",@"类型3", nil]];
  198.  
  199. }
  200. for (int j = ; j < +selectArray.count; j++)
  201. {
  202. UIButton *selectButton = (UIButton *)[self.view viewWithTag:j];
  203. if (j == selectIndex)
  204. {
  205. [selectButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
  206. [selectButton.titleLabel setFont:[UIFont systemFontOfSize:]];
  207. }
  208. else
  209. {
  210. [selectButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
  211. [selectButton.titleLabel setFont:[UIFont systemFontOfSize:]];
  212. }
  213. }
  214. }
  215. }
  216.  
  217. //请求最新数据成功
  218. -(void)requestJiuYuanNewFinish:(NSArray *)array
  219. {
  220. [self.tableArray removeAllObjects];
  221. [self.tableArray addObjectsFromArray:array];
  222. //刷新滚动列表
  223. [self addTableView];
  224. }
  225. @end

多个UITableView横向切换的简单实现(有点类似网易新闻)的更多相关文章

  1. 用jquery来实现类似“网易新闻”横向标题滑动的移动端页面

    HTML: <div id="navbar"> <div id='navbar_content' style="left:0px;"> ...

  2. 基于jQuery仿Flash横向切换焦点图

    给各网友分享一款基于jQuery仿Flash横向切换焦点图.利用Flash可以制作很多漂亮的图片相册应用,今天我们要用jQuery来实现这样的效果.它是一款仿Flash的横向图片切换焦点图插件,可以自 ...

  3. Swift - UITableView状态切换效果

    Swift - UITableView状态切换效果 效果 源码 https://github.com/YouXianMing/Swift-Animations // // TableViewTapAn ...

  4. AbstractRoutingDataSource 实现动态数据源切换原理简单分析

    AbstractRoutingDataSource 实现动态数据源切换原理简单分析 写在前面,项目中用到了动态数据源切换,记录一下其运行机制. 代码展示 下面列出一些关键代码,后续分析会用到 数据配置 ...

  5. 【转】 iOS开发 剖析网易新闻标签栏视图切换(addChildViewController属性介绍)

    原文:http://blog.csdn.net/hmt20130412/article/details/34523235 本来只是打算介绍一下addChildViewController这个方法的,正 ...

  6. iOS开发 剖析网易新闻标签栏视图切换(addChildViewController属性介绍)

    本文转载至 http://www.tuicool.com/articles/3ymMzub CSDN博客原文  http://blog.csdn.net/hmt20130412/article/det ...

  7. 转:addChildViewController实现网易新闻首页切换

    本来只是打算介绍一下addChildViewController这个方法的,正好今天朋友去换工作面试问到网易新闻标签栏效果的实现,就结合它,用个小Demo实例介绍一下:(具体解释都写在了Demo里面的 ...

  8. ViewPager图片切换的简单案例

    1)ViewPager类直接继承了ViewGroup类,所有它是一个容器类,可以在其中添加其他的view类. 2)ViewPager类需要一个PagerAdapter适配器类给它提供数据. 向右滑动切 ...

  9. ImageSwitcher图片切换的简单用例

    ImageSwitcher的原理:ImageSwitcher有两个子View:ImageView,当左右滑动的时候,就在这两个ImageView之间来回切换来显示图片 实现左右滑动切换图片 BaseA ...

随机推荐

  1. ALM11需求类型

  2. 与IO相关的等待事件troubleshooting-系列9

    Buffer Cache与IO相关的等待事件: 这种等待事件的产生原因是包含DBWR进程和IO Slaves的Buffer Cache操作. 'db file parallel write' , 'd ...

  3. Dijkstra in python

    下面是一段由python实现的Dijkstra算法,一些地方的处理实在非常棒,相比于C,代码的数量已经缩减到了60行,所以我想通过本文简单的介绍一下这段代码的细节之处,首先给出源程序: from sy ...

  4. mysql数据库全局只读和会话只读问题解析

    对于系统的发布是经常有的事情,有些时候需要隔绝外界对数据库的更改但是还要求可以读取数据,对于mss sql 这个就是很简单,直接属性—>选项—>只读 ,但是对于mysql这是不同的,今天仔 ...

  5. U盘安装SLES的方法

    安装准备: 一个4G 或 大于4G的U盘 也同样适合移动硬盘, SLES-11-DVD-i586-GM-DVD1.iso 文件1) 将U盘格式化成FAT32格式; 2) 下载 syslinux工具 h ...

  6. 教你50招提升ASP.NET性能(三):使用Microsoft的PDBs调试和分析外部的程序集和库

    (3)Use Microsoft’s PDBs to debug or profile external assemblies or libraries 招数3: 使用Microsoft的PDBs调试 ...

  7. Aizu 2304 Reverse Roads 费用流

    Reverse Roads Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view ...

  8. java基础复习之对于String对象,能够使用“=”赋值,也能够使用newkeyword赋值,两种方式有什么差别?

    String类型是实际工作中经经常使用到的类型,从数据类型上划分,String是一个引用类型,是API中定义的一个类.所以String类型的对象能够用new创建,比如String name=new S ...

  9. 应聘.net开发工程师常见的面试题(一)(转载)

    1. 简述 private. protected. public. internal 修饰符的访问权限. 答 . private : 私有成员, 在类的内部才可以访问. protected : 保护成 ...

  10. tar备份系统

    一.概述 前几天我通过SSH正在调戏汤姆猫(tomcat)的时候,服务器上CentOS突然挂了.开机grub,使用光盘linux rescue修复提示找不到linux分区,然后想mount硬盘备份系统 ...