iOS Dev > Learning-View controllers in the Cell

Source: Internet
Author: User

In every IOS developer's career, there are always times when you want to put a view controller in a TableView cell. Because this is a useful tool to deal with my various complex views and tedious operations in the view controller, it is easy to imagine that you want to heap some views on top of other views. Another common application scenario is to put CollectionView in the cell. Ideally, the CollectionView has its own controller so that the outside TableView controller is not affected by the associated view and each collection view cell data.

Because UIKit has many hooks (hook function) methods for communicating between components and behind the scenes, we need to make sure that the Uiviewcontroller container API is used to manage the child view controller, and if not, it might selves here fail or behave abnormally.

In this article I mainly talk about TableView and their cells, but these methods also apply to CollectionView.

For the sake of simplicity, make the view controller the only content in the cell. Instead of managing the root view of a single view controller, trying to manage a bunch of regular views can create unnecessary complexity. With one view controller and only one view controller per cell, the layout (at the cell level) is as simple as the following

Self.viewController.view.frame = Self.contentView.bounds;

The view controller is able to handle its own layout within. We can also put the height of the calculation into the view controller inside.

There are two ways to do this: You can hold one view controller per cell, or you can manage these view controllers at the controller layer.

Each cell holds a view controller

If we put a view controller in each cell, we can lazily load it in this cell.

-(Skcontentviewcontroller *) Contentviewcontroller {

if (!_contentviewcontroller) {

Skviewcontroller *contentviewcontroller = [[Skcontentviewcontroller alloc] init];

Self.contentviewcontroller = Contentviewcontroller;

}

return _contentviewcontroller;

}

Remember that we are not adding the view controller's root view as a sub-view to our cell's contentview. When the cell needs to be configured in the-cellforrowatindexpath: method, we can pass our model to the controller and it will configure itself according to the latest content. Since these cells are multiplexed, your controller must be designed to completely reset itself at any time as long as its model changes.

UITableView gave us the cell display before and after the hooks and before and after the removal. We want to add the cell's view controller to our parent table View Controller at this time and add the cell view controller's root view to the cell.

-(void) TableView: (UITableView *) TableView Willdisplaycell: (UITableViewCell *) cell Forrowatindexpath: (Nsindexpath *) Indexpath {

[Cell addviewcontrollertoparentviewcontroller:self];

}

-(void) TableView: (UITableView *) TableView Didenddisplayingcell: (UITableViewCell *) cell Forrowatindexpath: ( Nsindexpath *) Indexpath {

[Cell Removeviewcontrollerfromparentviewcontroller];

}

In the cell class, implement these methods to generate a simple view controller container.

-(void) Addviewcontrollertoparentviewcontroller: (Uiviewcontroller *) Parentviewcontroller {

[Parentviewcontroller Contentviewcontroller];

[Self.contentviewcontroller Didmovetoparentviewcontroller:parentviewcontroller];

[Self.contentview AddSubview:self.contentViewController.view];

}

-(void) Removeviewcontrollerfromparentviewcontroller {

[Self.contentViewController.view Removefromsuperview];

[Self.contentviewcontroller Willmovetoparentviewcontroller:nil];

[Self.contentviewcontroller Removefromparentviewcontroller];

}

After the view controller joins as a child view controller, add the Subview to the view and confirm that-viewwillappear: The method can be called correctly. TableView's Willdisplaycell: The method is corresponding to the display method (-viewwillappear: And-viewdidappear:), and the-didenddisplayingcell: The method corresponds to the vanishing square, So we can show our containers in these ways.

holding the view controller in the parent container

Each cell has its own view controller that works fine, but it feels a little weird. In the Cocoa MVC pattern, models and views should not be aware of the view controller they are using, and having a cell (actually a UIView) hold a controller violates this rule system. To solve this problem, we can hold all child view controllers at the parent container level in the table View controller.

We have two ways to accomplish this task, (the simpler way is) we can pre-generate a view controller (and a view) for each item in the table we need to show, or generate a view controller when needed, and then loop through them, like UITableView to the cell Reuse (This is more difficult). Start with a simple way, when the IPhone first appears, the device is limited by memory and cannot generate a view for each row in the table. Now our device has more memory, so you might not need to reuse the view when you only need to show very few rows.

-(void) Setupchildviewcontrollers {

Self.contentviewcontrollers = [Self.modelobjects arraybytransformingobjectsusingblock:^id (SKModel *model) {

Skviewcontroller *contentviewcontroller = [[Skcontentviewcontroller alloc] Initwithmodel:model];

[Self addchildcontentviewcontroller:contentviewcontroller];

return contentviewcontroller;

}];

}

-(void) Addchildcontentviewcontroller: (Uiviewcontroller *) Childcontroller {

[Self addchildviewcontroller:childcontroller];

[Childcontroller didmovetoparentviewcontroller:self];

}

(I used the-arraybytransformingobjectsusingblock in Objective-shorthand: The method, the so-called-map:)

Once you have the view controller, you can put their view into the cell in method-cellforrowatindexpath:

-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath {

UITableViewCell *cell =//make a cell

Skviewcontroller *viewcontroller = Self.contentviewcontrollers[indexpath.row];

Cell.hostedview = Contentviewcontroller.view;

return cell;

}

Inside the cell, you can get the Hostedview, add it to the child view, and clear it when you reuse it.

-(void) Sethostedview: (UIView *) Hostedview {

_hostedview = Hostedview;

[Self.contentview Addsubview:hostedview];

}

-(void) Prepareforreuse {

[Super Prepareforreuse];

[Self.hostedview Removefromsuperview];

Self.hostedview = nil;

}

This is the simple way to do all the things you have to do. In order to use the view controller for reuse, a nsmutableset type of unusedviewcontrollers and a nsmutabledictionary type of viewcontrollersbyindexpath are required.

-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath {

UITableViewCell *cell =//make a cell

Skviewcontroller *viewcontroller = [self recycledornewviewcontroller];

Viewcontroller.model = [Self.datasource Objectatindexpath:indexpath];

Self.viewcontrollersbyindexpath[indexpath] = Viewcontroller;

Cell.hostedview = Contentviewcontroller.view;

return cell;

}

-(Uiviewcontroller *) Recycledornewviewcontroller {

if (Self.unusedViewControllers.count > 1) {

Uiviewcontroller *viewcontroller = [Self.unusedviewcontrollers anyobject];

[Self.unusedviewcontrollers Removeobject:viewcontroller];

return viewcontroller;

}

Skviewcontroller *contentviewcontroller = [[Skcontentviewcontroller alloc] init];

[Self addchildviewcontroller:contentviewcontroller];

return contentviewcontroller;

}

-(void) TableView: (UITableView *) TableView Didenddisplayingcell: (UITableViewCell *) cell Forrowatindexpath: ( Nsindexpath *) Indexpath {

Uiviewcontroller *viewcontroller = Self.viewcontrollersbyindexpath[indexpath];

[Self.viewcontrollersbyindexpath Removeobjectforkey:indexpath]

[Self.unusedviewcontrollers Addobject:viewcontroller];

}

-(Nsmutableset *) unusedviewcontrollers {

if (!_unusedviewcontrollers) {

Self.unusedviewcontrollers = [Nsmutableset set];

}

return _unusedviewcontrollers;

}

-(Nsmutabledictionary *) Viewcontrollersbyindexpath {

if (!_viewcontrollersbyindexpath) {

Self.viewcontrollersbyindexpath = [Nsmutabledictionary dictionary];

}

return _viewcontrollersbyindexpath;

}

There are three important things, one, unusedviewcontrollers is loaded with all the view controllers waiting to be reused, two, viewcontrollersbyindexpaths is loaded with all the view controllers in use (we must hold them, Otherwise it will be destroyed). Finally, the cell only touches the hostedview of the view controller, which conforms to our previous view and does not know the view controller principle.

This is the two best way I've found to fit the Uiviewcontroller object into the cell. If I miss out on any technology I'd be happy to listen.

English Source: khanlou.com

This article by Bole Online-Above translation, NATHANW School Draft

Link: http://ios.jobbole.com/82123/

iOS Dev > Learning-View controllers in the Cell

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.