发布于 2015-12-29 07:54:01 | 226 次阅读 | 评论: 0 | 来源: 分享

这里有新鲜出炉的精品教程,程序狗速度看过来!

iOS苹果移动操作系统

苹果iOS是由苹果公司开发的移动操作系统。苹果公司最早于2007年1月9日的Macworld大会上公布这个系统,最初是设计给iPhone使用的,后来陆续套用到iPod touch、iPad以及Apple TV等产品上。


一、多张表视图的内存问题解决方案

 

借鉴TableView中Cell的重用机制,我们就把之前的Demo中ScrollView上的TableView进行复用,在我的博客中用的是两个TableView进行的交叉复用,当然你也可以用其他个数的TableView进行复用。下面是实例化ScrollView上的TableView的代码,由下面的代码可以看出只实例化2个TableView, 并且把初始化后的TableView放在了TableView的初始化的位置上。而在原来的Demo中  -(void) initDownTables 方法会实例化多个TableView, 这也是内存问题的根源。


  #pragma mark --初始化下方的TableViews
  -(void) initDownTables{

      for (int i = 0; i 2; i ++) {

          UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(i * _mViewFrame.size.width, 0, _mViewFrame.size.width, _mViewFrame.size.height - TOPHEIGHT)];
          tableView.delegate = self;
          tableView.dataSource = self;
          tableView.tag = i;

         [_scrollTableViews addObject:tableView];
         [_scrollView addSubview:tableView];
     }

 }

上面的代码减少了TableView的实例化,那么我们如何进行复用呢? 我个人采取的是改变TableView在ScrollView上的Frame, 并且刷新相应的TableView, 下面的代码是把TableView移动到当前显示页数,并且刷新TableView上的数据。代码如下:


  #pragma mark --根据scrollView的滚动位置复用tableView,减少内存开支
  -(void) updateTableWithPageNumber: (NSUInteger) pageNumber{
      int tabviewTag = pageNumber % 2;

      CGRect tableNewFrame = CGRectMake(pageNumber * _mViewFrame.size.width, 0, _mViewFrame.size.width, _mViewFrame.size.height - TOPHEIGHT);

      UITableView *reuseTableView = _scrollTableViews[tabviewTag];
      reuseTableView.frame = tableNewFrame;
      [reuseTableView reloadData];
 }

上面的方法在那调用呢? 我是在ScrollView到达相应的页数时进行tableView的移动和数据的刷新。具体的调用代理方法如下:


  - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView

  {
      if ([scrollView isEqual:_scrollView]) {
          _currentPage = _scrollView.contentOffset.x/_mViewFrame.size.width;

          _currentPage = _scrollView.contentOffset.x/_mViewFrame.size.width;

          //    UITableView *currentTable = _scrollTableViews[_currentPage];
         //    [currentTable reloadData];

         [self updateTableWithPageNumber:_currentPage];

         return;
     }
     [self modifyTopScrollViewPositiong:scrollView];
 }

上面的代码就可以实现TableView的复用了,并且减少了内存开支。如有更好的解决方案,还请提出,会及时的进行修改和改正。不希望大家只是“吐槽”和提出一些问题,我期待和大家交流和学习的是一些问题更好的解决方案。

二、头部按钮达到一定数量时,布局的显示方案。

也是防新闻头条的那种,按钮多到一定个数时回使用ScrollView进行滚动。在本Demo中是超过6个按钮就可以滑动,而6个以下是平分整个屏幕的宽度的。主要做的修改是把Button放到ScrollView上,找准时机,让ScorllView进行滑动。下方的代码是实例化TopScrollView,并把按钮放到TopScrollView上:


  #pragma mark -- 实例化顶部的tab
  -(void) initTopTabs{
      CGFloat width = _mViewFrame.size.width / 6;

      if(self.tabCount 6){
          width = _mViewFrame.size.width / self.tabCount;
      }

      _topMainView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, _mViewFrame.size.width, TOPHEIGHT)];

     _topScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, _mViewFrame.size.width, TOPHEIGHT)];

     _topScrollView.showsHorizontalScrollIndicator = NO;

     _topScrollView.showsVerticalScrollIndicator = YES;

     _topScrollView.bounces = NO;

     _topScrollView.delegate = self;

     if (_tabCount >= 6) {
         _topScrollView.contentSize = CGSizeMake(width * _tabCount, TOPHEIGHT);

     } else {
         _topScrollView.contentSize = CGSizeMake(_mViewFrame.size.width, TOPHEIGHT);
     }

     [self addSubview:_topMainView];

     [_topMainView addSubview:_topScrollView];

     for (int i = 0; i ) {

         UIView *view = [[UIView alloc] initWithFrame:CGRectMake(i * width, 0, width, TOPHEIGHT)];

         view.backgroundColor = [UIColor lightGrayColor];

         if (i % 2) {
             view.backgroundColor = [UIColor grayColor];
         }

         UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, width, TOPHEIGHT)];
         button.tag = i;
         [button setTitle:[NSString stringWithFormat:@"按钮%d", i+1] forState:UIControlStateNormal];
         [button addTarget:self action:@selector(tabButton:) forControlEvents:UIControlEventTouchUpInside];
         [view addSubview:button];

         [_topViews addObject:view];
         [_topScrollView addSubview:view];
     }
 }

以上内容实在之前的Demo的基础上做的简单的修改,Demo还在完善中,后期会加上网络请求,本地缓存等。发表博客的初衷是与大家进行交流和学习,而不是看一些人进行吐槽。问题是在所难免,希望大家能提出问题所在,给出自己的解决方案,进行交流,共同进步。下方是Demo运行的效果:

上面是运行结果截图,下方是层次截图:

把新的代码更新到了GitHub上,优化还在继续,欢迎大家批评指正。
Demo在GitHub上的分享地址:https://github.com/lizelu/SliderTabBar



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务