First, affix the non-encapsulated code, so that the comparison with the back
@interface mthomedropdown: UIView
+ (instancetype) dropdown;
@property (nonatomic, strong) Nsarray *categories;
@end
1.categories only one type of data is passed in and cannot be used in multiple places
2. Once encapsulated, it can be used for multiple data passing in multiple places
Two. Encapsulation
@class mthomedropdown;
@protocol Mthomedropdowndatasource <nsobject>
/**
* How many lines are there in the left table?
*/
-(Nsinteger) numberofrowsinmaintable: (mthomedropdown *) homedropdown;
/**
* title of each row in the table on the left
* @param row number
*/
-(nsstring *) Homedropdown: (mthomedropdown *) Homedropdown titleforrowinmaintable: (int) row;
/**
* Sub-data for each row in the table on the left
* @param row number
*/
-(nsarray *) Homedropdown: (mthomedropdown *) Homedropdown subdataforrowinmaintable: (int ) row;
@optional
/**
* icons for each row on the left table
* @param row number
*/
-(nsstring *) Homedropdown: (mthomedropdown *) Homedropdown iconforrowinmaintable: (int) row;
/**
* the selected icon for each row in the table on the left
* @param row number
*/
-(nsstring *) Homedropdown: (mthomedropdown *) Homedropdown selectediconforrowinmaintable: (int ) row;
@end
@interface mthomedropdown: UIView
+ (instancetype) dropdown;
@property (nonatomic, weak) ID<mthomedropdowndatasource> dataSource;
@end
1. Declaration Agreement:<Mthomedropdowndatasource, and the method of declaring the agreement
2. Declare compliance with the data source dataSource of the agreement.
3.. m
/** the line number selected on the left main table * /
@property (nonatomic, assign) nsinteger selectedmainrow;
4. Call the DataSource method to get the data
-(Nsinteger) TableView: (uitableview *) TableView numberofrowsinsection: (nsinteger) section
{
if (TableView = = self . Maintableview) {
return [self. DataSource numberofrowsinmaintable:self];
} Else {
return [self. DataSource homedropdown:self -subdataforrowinmaintable: Self. Selectedmainrow];
}
}
5. Create Mtmetatool Metadata Tool Classes : managing all metadata ( fixed descriptive data )
. h
/**
* return to 344 City
*/
+ (nsarray *) cities;
/**
* return all categorical data
*/
+ (nsarray *) categories;
/**
* return all sort data
*/
+ (nsarray *) sorts;
. m
Static nsarray *_cities;
+ (nsarray *) Cities
{
if (_cities = = Nil) {
_cities = [mtcity objectarraywithfilename:@ "Cities.plist"];;
}
return _cities;
}
Static nsarray *_categories;
+ (nsarray *) Categories
{
if (_categories = = Nil) {
_categories = [mtcategory objectarraywithfilename:@ "Categories.plist"];;
}
return _categories;
}
Static nsarray *_sorts;
+ (nsarray *) sorts
{
if (_sorts = = Nil) {
_sorts = [mtsort objectarraywithfilename:@ "Sorts.plist"];;
}
return _sorts;
}
6. A Zxcategoryviewcontroller class wants to use Homedropdown to display the data, it is necessary to follow the protocol and implement the method
@interface Zxcategoryviewcontroller () <mthomedropdowndatasource>
Dropdown. DataSource = self;
#pragma mark-mthomedropdowndatasource
-(Nsinteger) numberofrowsinmaintable: (mthomedropdown *) homedropdown
{
return [mtmetatool categories]. Count;
}
-(nsstring *) Homedropdown: (mthomedropdown *) Homedropdown titleforrowinmaintable: (int ) Row
{
mtcategory *category = [mtmetatool categories][row];
return category. Name;
}
-(nsstring *) Homedropdown: (mthomedropdown *) Homedropdown iconforrowinmaintable: (int ) Row
{
mtcategory *category = [mtmetatool categories][row];
return category. Small_icon;
}
-(nsstring *) Homedropdown: (mthomedropdown *) Homedropdown selectediconforrowinmaintable: ( int) Row
{
mtcategory *category = [mtmetatool categories][row];
return category. Small_highlighted_icon;
}
-(nsarray *) Homedropdown: (mthomedropdown *) Homedropdown subdataforrowinmaintable: (int ) Row
{
mtcategory *category = [mtmetatool categories][row];
return category. Subcategories;
}
IOS Mimic TableView Package