前幾天發佈了一篇iOS開發之多表視圖滑動切換示例(仿"頭條"客戶端)的博客,之因此寫這篇博客,是由於一位iOS初學者提了一個問題,簡單的寫了個demo作了個示範,讓其在基礎上作擴展和改進。被CocoaChina中iOS模塊所收錄實在出乎個人意料,連接地址(http://www.cocoachina.com/ios/20150706/12370.html),在CocoaChina上看了下面的評論,Demo的問題確實有,優化和改進的空間也是蠻大的。首先內存問題是必須考慮的,不能把這麼多的TabalView實例化後添加到ScrollView上,只要是作過iOS的小夥伴這個問題應該不難看出。再一個是頭部按鈕多了之後會擠在一塊兒,還有若是添加上網絡請求的話,沒作本地緩存,等一系列的問題。html
在今天的博客中要作兩個優化。第一:多個TableView的內存問題。第二:頭部多個按鈕的顯示問題。今天的博客的內容是在上一篇博客iOS開發之多表視圖滑動切換示例(仿"頭條"客戶端)作的優化和擴展,同時也會在gitHub上更新一下Demo的代碼,廢話很少說,開始今天博客的主題。ios
1、多張表視圖的內存問題解決方案git
借鑑TableView中Cell的重用機制,咱們就把以前的Demo中ScrollView上的TableView進行復用,在個人博客中用的是兩個TableView進行的交叉複用,固然你也能夠用其餘個數的TableView進行復用。下面是實例化ScrollView上的TableView的代碼,由下面的代碼能夠看出只實例化2個TableView, 而且把初始化後的TableView放在了TableView的初始化的位置上。而在原來的Demo中 -(void) initDownTables 方法會實例化多個TableView, 這也是內存問題的根源。github
1 #pragma mark --初始化下方的TableViews 2 -(void) initDownTables{ 3 4 for (int i = 0; i < 2; i ++) { 5 6 UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(i * _mViewFrame.size.width, 0, _mViewFrame.size.width, _mViewFrame.size.height - TOPHEIGHT)]; 7 tableView.delegate = self; 8 tableView.dataSource = self; 9 tableView.tag = i; 10 11 [_scrollTableViews addObject:tableView]; 12 [_scrollView addSubview:tableView]; 13 } 14 15 }
上面的代碼減小了TableView的實例化,那麼咱們如何進行復用呢? 我我的採起的是改變TableView在ScrollView上的Frame, 而且刷新相應的TableView, 下面的代碼是把TableView移動到當前顯示頁數,而且刷新TableView上的數據。代碼以下:緩存
1 #pragma mark --根據scrollView的滾動位置複用tableView,減小內存開支 2 -(void) updateTableWithPageNumber: (NSUInteger) pageNumber{ 3 int tabviewTag = pageNumber % 2; 4 5 CGRect tableNewFrame = CGRectMake(pageNumber * _mViewFrame.size.width, 0, _mViewFrame.size.width, _mViewFrame.size.height - TOPHEIGHT); 6 7 UITableView *reuseTableView = _scrollTableViews[tabviewTag]; 8 reuseTableView.frame = tableNewFrame; 9 [reuseTableView reloadData]; 10 }
上面的方法在那調用呢? 我是在ScrollView到達相應的頁數時進行tableView的移動和數據的刷新。具體的調用代理方法以下:網絡
1 - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView 2 3 { 4 if ([scrollView isEqual:_scrollView]) { 5 _currentPage = _scrollView.contentOffset.x/_mViewFrame.size.width; 6 7 _currentPage = _scrollView.contentOffset.x/_mViewFrame.size.width; 8 9 // UITableView *currentTable = _scrollTableViews[_currentPage]; 10 // [currentTable reloadData]; 11 12 [self updateTableWithPageNumber:_currentPage]; 13 14 return; 15 } 16 [self modifyTopScrollViewPositiong:scrollView]; 17 }
上面的代碼就能夠實現TableView的複用了,而且減小了內存開支。若有更好的解決方案,還請提出,會及時的進行修改和改正。不但願你們只是「吐槽"和提出一些問題,我期待和你們交流和學習的是一些問題更好的解決方案。ide
2、頭部按鈕達到必定數量時,佈局的顯示方案。佈局
也是防新聞頭條的那種,按鈕多到必定個數時回使用ScrollView進行滾動。在本Demo中是超過6個按鈕就能夠滑動,而6個如下是平分整個屏幕的寬度的。主要作的修改是把Button放到ScrollView上,找準時機,讓ScorllView進行滑動。下方的代碼是實例化TopScrollView,並把按鈕放到TopScrollView上:post
1 #pragma mark -- 實例化頂部的tab 2 -(void) initTopTabs{ 3 CGFloat width = _mViewFrame.size.width / 6; 4 5 if(self.tabCount <=6){ 6 width = _mViewFrame.size.width / self.tabCount; 7 } 8 9 _topMainView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, _mViewFrame.size.width, TOPHEIGHT)]; 10 11 _topScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, _mViewFrame.size.width, TOPHEIGHT)]; 12 13 _topScrollView.showsHorizontalScrollIndicator = NO; 14 15 _topScrollView.showsVerticalScrollIndicator = YES; 16 17 _topScrollView.bounces = NO; 18 19 _topScrollView.delegate = self; 20 21 if (_tabCount >= 6) { 22 _topScrollView.contentSize = CGSizeMake(width * _tabCount, TOPHEIGHT); 23 24 } else { 25 _topScrollView.contentSize = CGSizeMake(_mViewFrame.size.width, TOPHEIGHT); 26 } 27 28 29 [self addSubview:_topMainView]; 30 31 [_topMainView addSubview:_topScrollView]; 32 33 34 35 for (int i = 0; i < _tabCount; i ++) { 36 37 UIView *view = [[UIView alloc] initWithFrame:CGRectMake(i * width, 0, width, TOPHEIGHT)]; 38 39 view.backgroundColor = [UIColor lightGrayColor]; 40 41 if (i % 2) { 42 view.backgroundColor = [UIColor grayColor]; 43 } 44 45 UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, width, TOPHEIGHT)]; 46 button.tag = i; 47 [button setTitle:[NSString stringWithFormat:@"按鈕%d", i+1] forState:UIControlStateNormal]; 48 [button addTarget:self action:@selector(tabButton:) forControlEvents:UIControlEventTouchUpInside]; 49 [view addSubview:button]; 50 51 52 [_topViews addObject:view]; 53 [_topScrollView addSubview:view]; 54 } 55 }
以上內容實在以前的Demo的基礎上作的簡單的修改,Demo還在完善中,後期會加上網絡請求,本地緩存等。發表博客的初衷是與你們進行交流和學習,而不是看一些人進行吐槽。問題是在所不免,但願你們能提出問題所在,給出本身的解決方案,進行交流,共同進步。下方是Demo運行的效果:學習
上面是運行結果截圖,下方是層次截圖:
把新的代碼更新到了GitHub上,優化還在繼續,歡迎你們批評指正。
Demo在GitHub上的分享地址:https://github.com/lizelu/SliderTabBar