Drop-down screen control

Drop-down screen control
cover.Jpg

Demo effect:

Drop-down screen control
example.gif

Engineering structure drawing:

Drop-down screen control
structure.Png

1 is used to simulate the assembly data, because it may be multi-layered, so we are here to assemble the data. In MMBaseItem we define three enumerations:

This field / / after we temporarily keep expanding coverage, you may want some options can not choose, display gray typedef NS_ENUM (NSUInteger, MMPopupViewMarkType) {/ / selected MMPopupViewDisplayTypeSelected = 0, MMPopupViewDisplayTypeUnselected = 1 / / can be selected, you can select / not}; typedef NS_ENUM (NSUInteger, MMPopupViewSelectedType) {/ / support or radio select MMPopupViewSingleSelection, MMPopupViewMultilSeMultiSelection} / / / / radio, multi NS_ENUM (NSUInteger, typedef; MMPopupViewDisplayType) {/ / resolution play type view MMPopupViewDisplayTypeNormal = 0, MMPopupViewDisplayTypeMultilayer = 1 / A, two / / MMPopupViewDisplayTypeFilters = 2}, / / mixed;

MMItem provides a common interface, pop-up view driven by the corresponding MMItem subclass data. Because MMPopupViewDisplayTypeNormal and MMPopupViewDisplayTypeMultilayer two types of layout is relatively simple, so the layout object is only useful in MMPopupViewDisplayTypeFilters.

Of course, I’m here to simulate data. An idea of building tree model is given:

Drop-down screen control
screen snapshot 2016-12-22 PM 11.14.13.png

1.1 first we put the root node in the queue

Drop-down screen control
1.1.png

1.2 according to the A holds a pointer to the child node B, C into the queue, equivalent to the B, C added to the A childrenNodes. Then move the A to the queue.

Drop-down screen control
1.2

1.3 then traverses each node in accordance with the above logic one by one. Until the queue is empty, stands for a build. The following diagram does not give the full, only partial state of the moment.

Drop-down screen control
1.3.png
Drop-down screen control
1.4.png
Drop-down screen control
1.5.png

2 initialization view:

MMComBoBoxView *view = [[MMComBoBoxView alloc] initWithFrame:CGRectMake (0, 64, kScreenWidth, 40); view.dataSource = self; view.delegate = self; [self.view addSubview:view]; [view reload];

3 through the datasource protocol to the data to the MMComBoBoxView, you can think of the UITableView data driver can be.

#pragma mark MMComBoBoxViewDataSource (NSUInteger): numberOfColumnsIncomBoBoxView (MMComBoBoxView * comBoBoxView) {return self.mutableArray.count;} - (MMItem *) comBoBoxView: (* MMComBoBoxView) comBoBoxView infomationForColumn: (NSUInteger) column {return self.mutableArray[column];}

4 we will through the MMComBoBoxViewDelegate protocol to check out the callback path, here we choose the essence of array is the callback storage path may be developers to upload more than title, there may be code corresponding to a series of fields. So easy to expand.

#pragma mark - MMComBoBoxViewDelegate - (void) comBoBoxView: (MMComBoBoxView * comBoBoxViewd) didSelectedItemsPackagingInArray: (NSArray *) array atIndex: (NSUInteger index) {MMItem *rootItem = self.mutableArray[index]; switch (rootItem.displayType) {case MMPopupViewDisplayTypeNormal: case MMPopupViewDisplayTypeMultilayer:{NSMutableString *title / / splicing option = [NSMutableString string]; __block NSInteger firstPath; [array enumerateObjectsUsingBlock:^ (MMSelectedPath * path, NSUInteger IDX. BOOL * _Nonnull stop) {[title appendString:idx? [NSString stringWithFormat:@, [rootItem findTitleBySelectedPath:path]] ";% @": [rootItem findTitleBySelectedPath:path]]; If (IDX = = 0) {firstPath = path.firstPath;}}]; NSLog (@ "% @ when title is, the selected field is rootItem.title% @", title); break;} case MMPopupViewDisplayTypeFilters:{MMCombinationItem * combineItem = (MMCombinationItem * rootItem); [array enumerateObjectsUsingBlock:^ (NSMutableArray* _Nonnull subArray, NSUInteger IDX, BOOL * _Nonnull stop if (combineItem.isHasSwitch) {& & IDX = = 0) {for (MMSelectedPath *path in subArray) {MMAlternativeItem = combineItem.alternativeArray[path.firstPath] * alternativeItem; NSLog (@ "when Title:% @, selected shape State:%d, alternativeItem.title, alternativeItem.isSelected);}} return; NSString *title; NSMutableString *subtitles = [NSMutableString string]; for (MMSelectedPath *path in subArray MMItem combineItem.childrenNodes [path.firstPath]) {*firstItem = MMItem; *secondItem = combineItem.childrenNodes[path.firstPath].childrenNodes[path.secondPath]; title = firstItem.title; [subtitles appendString:[NSString stringWithFormat:@, secondItem.title]] NSLog;}% @ (@ "when title the selected field is% @," title, subtitles,% @);} Break;} default: break;}}

If you think this article is helpful to you, welcome to like or star!
demo address