Example of sliding switching between multiple table views in iOS development (imitation & quot; headlines & quot; client) --- optimization (1), ios example

Source: Internet
Author: User

Example of sliding switching between multiple table views in iOS development (similar to the "toutiao" client) --- optimization (1), ios example

A few days ago, I published a blog titled sliding switching examples of multiple table views (similar to the "toutiao" client) for iOS development. The reason why I wrote this blog is that an iOS beginner raised a question, I wrote a demo to demonstrate how to scale and improve it. By CocoaChina iOS module included is really beyond my expectation, link address (http://www.cocoachina.com/ios/20150706/12370.html), in CocoaChina read the following comments, Demo problems indeed have, the space for optimization and improvement is also quite large. First of all, the memory problem must be considered. You cannot instantiate so many tabalviews and add them to ScrollView. As long as it is a friend of iOS, it is not difficult to see this problem. Another problem is that when there are more headers, the buttons will be crowded together, and if network requests are added, local cache will not be performed, and a series of problems will occur.

Two optimizations are required in today's blog. First: memory problems of multiple tableviews. Second, the display of multiple buttons in the header is incorrect. The content of today's blog is optimized and extended by sliding switching examples of table views (similar to the "toutiao.com" client) developed in iOS in the previous blog, at the same time, the Demo code will be updated on gitHub. If you don't talk much about it, start the topic of today's blog.

I. Solutions to memory problems in multiple table views

Using the Cell reuse mechanism in TableView, We will reuse the TableView on ScrollView in the previous Demo. In my blog, we will use two tableviews for cross-reuse, of course, you can also use TableView with other numbers for reuse. The following is the code for instantiating TableView on ScrollView. The code below shows that only two tableviews are instantiated, And the initialized TableView is placed at the initialization position of TableView. In the original Demo, the-(void) initdownables method will instantiate multiple tableviews, which is also the root cause of memory problems.

1 # pragma mark -- initialize TableViews 2 below (void) initdownables {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}

  

The code above reduces the instantiation of TableView. How can we reuse it? I personally changed the Frame of TableView on ScrollView and refreshed the corresponding TableView. The code below is to move TableView to the current display page number and refresh the data on TableView. The Code is as follows:

1 # pragma mark -- reuse tableView Based on the rolling position of scrollView to reduce memory expenditure. 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}

 

Where is the above method called? When ScrollView reaches the corresponding page number, I move the table view and refresh the data. The Calling proxy method is as follows:

 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 }

 

The code above can achieve TableView reuse, and reduce memory spending. If there is a better solution, please submit it and make modifications and corrections in a timely manner. I don't want you to just "Speak Out" and raise some questions. I am looking forward to communicating with you and learning better solutions to some problems.

 

2. layout display scheme when the number of buttons in the header reaches a certain value.

It is also the kind of anti-toutiao that uses ScrollView to scroll back when there are a certain number of buttons. In this Demo, if there are more than 6 buttons, you can slide, and if there are less than 6 buttons, the width of the entire screen is evenly divided. The main modification is to put the Button on the ScrollView to identify the time and let ScorllView slide. The code below is to instantiate the TopScrollView and put the button on the TopScrollView:

1 # pragma mark -- instantiate the top 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: @ "button % 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}

 

The above content is a simple modification based on the previous Demo. The Demo is still in progress, and network requests and local cache will be added later. The original intention of posting a blog is to communicate and learn with you, rather than reading some people. The problem is inevitable. I hope you can raise the problem, give your own solutions, communicate with each other, and make common progress. Below is the Demo running effect:

  

Update the new Code to GitHub, and the optimization continues. You are welcome to criticize and correct it.
Demo share on GitHub: https://github.com/lizelu/SliderTabBar

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.