标签:
#import <UIKit/UIKit.h> @class XWDropdownMenu; #pragma mark 数据源方法 @protocol XWDropdownMenuDataSource <NSObject> @required /** * 主表格一共有多少行 */ - (NSInteger)numberOfRowsInMainTable:(XWDropdownMenu *)dropdownMenu; /** * 主表格每一行的标题 * @param row 行号 */ - (NSString *)dropdownMenu:(XWDropdownMenu *)dropdownMenu titleForRowInMainTable:(NSInteger)row; /** * 主表格每一行的子数据 数组 * @param row 行号 */ - (NSArray *)dropdownMenu:(XWDropdownMenu *)dropdownMenu subdataForRowInMainTable:(NSInteger)row; @optional /** * 主表格每一行 分类数量 * @param row 行号 */ - (NSString *)dropdownMenu:(XWDropdownMenu *)dropdownMenu countForRowInMainTable:(NSInteger)row; /** * 子表格每一行子数据 分类数量 * @param row 行号 */ - (NSArray *)dropdownMenu:(XWDropdownMenu *)dropdownMenu subdataCountForRowInSubTable:(NSInteger)row; /** * 主表格每一行的图标 * @param row 行号 */ - (NSString *)dropdownMenu:(XWDropdownMenu *)dropdownMenu iconForRowInMainTable:(NSInteger)row; /** * 子表格每一行的选中图标 * @param row 行号 */ - (NSString *)dropdownMenu:(XWDropdownMenu *)dropdownMenu selectedIconForRowInMainTable:(NSInteger)row; @end #pragma mark 代理方法 @protocol XWDropdownMenuDelegate <NSObject> @optional - (void)dropdownMenu:(XWDropdownMenu *)dropdownMenu didSelectRowInMainTable:(NSInteger)row; - (void)dropdownMenu:(XWDropdownMenu *)dropdownMenu didSelectRowInSubTable:(NSInteger)subrow inMainTable:(NSInteger)mainRow; @end #pragma mark 构造方法 @interface XWDropdownMenu : UIView + (instancetype)dropdownMenu; -(void)refreshMenu; @property (nonatomic, weak) id<XWDropdownMenuDataSource> dataSource; @property (nonatomic, weak) id<XWDropdownMenuDelegate> delegate; /**主表被选中的字体颜色,默认是黑色*/ @property (strong, nonatomic) UIColor *selectedRowTextColor; /**该属性设置成YES,主表比较小*/ @property (nonatomic, assign)BOOL isMainTableSmall; @end
#import "XWDropdownMenu.h" #import "XWDropdownMenuMainCell.h" #import "XWDropdownMenuSubCell.h" #import "XWBadgeView.h" #define XWColor(r, g, b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1.0] #define XWUserDefaults [NSUserDefaults standardUserDefaults] #define kXWLastSelectedMainRow @"XWLastSelectedMainRow" /** 值越大,主表越小 */ #define kXWTableScale 0.15 @interface XWDropdownMenu() @property (weak, nonatomic) IBOutlet UITableView *mainTableView; @property (weak, nonatomic) IBOutlet UITableView *subTableView; /** 左边主表选中的行号 */ @property (nonatomic, assign) NSInteger selectedMainRow; /** 表格比例 */ @property (weak, nonatomic) IBOutlet NSLayoutConstraint *tableScale; @end @implementation XWDropdownMenu + (instancetype)dropdownMenu { return [[[NSBundle mainBundle] loadNibNamed:@"XWDropdownMenu" owner:nil options:nil] firstObject]; } - (void)awakeFromNib { self.subTableView.backgroundColor = XWColor(245, 245, 245); self.selectedMainRow = [[XWUserDefaults objectForKey:kXWLastSelectedMainRow] integerValue]; self.selectedRowTextColor = [UIColor blackColor]; } #pragma mark - 数据源方法 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if (tableView == self.mainTableView) { if ([self.dataSource respondsToSelector:@selector(numberOfRowsInMainTable:)]) { return [self.dataSource numberOfRowsInMainTable:self]; } } else { if ([self.dataSource respondsToSelector:@selector(dropdownMenu:subdataForRowInMainTable:)]) { return [self.dataSource dropdownMenu:self subdataForRowInMainTable:self.selectedMainRow].count; } } return 0; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = nil; //主表 if (tableView == self.mainTableView) { cell = [XWDropdownMenuMainCell cellWithTableView:tableView]; // 取出数据 if ([self.dataSource respondsToSelector:@selector(dropdownMenu:titleForRowInMainTable:)]) { cell.textLabel.text = [self.dataSource dropdownMenu:self titleForRowInMainTable:indexPath.row]; cell.textLabel.font = [UIFont systemFontOfSize:14]; if (self.selectedMainRow == indexPath.row) { [tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone]; cell.textLabel.textColor = self.selectedRowTextColor; }else{ cell.textLabel.textColor = [UIColor blackColor]; } } if ([self.dataSource respondsToSelector:@selector(dropdownMenu:iconForRowInMainTable:)]) { cell.imageView.image = [UIImage imageNamed:[self.dataSource dropdownMenu:self iconForRowInMainTable:indexPath.row]]; } if ([self.dataSource respondsToSelector:@selector(dropdownMenu:selectedIconForRowInMainTable:)]) { cell.imageView.highlightedImage = [UIImage imageNamed:[self.dataSource dropdownMenu:self selectedIconForRowInMainTable:indexPath.row]]; } if ([self.dataSource respondsToSelector:@selector(dropdownMenu:countForRowInMainTable:)]) { XWBadgeView *badgeView = [[XWBadgeView alloc] initWithFrame:CGRectMake(0, 0, 0, 13)]; badgeView.backgroundColor = XWColor(205, 205, 205); badgeView.textColor = [UIColor whiteColor]; badgeView.font = [UIFont systemFontOfSize:12]; badgeView.badgeValue = [self.dataSource dropdownMenu:self countForRowInMainTable:indexPath.row]; cell.accessoryView = badgeView; } } else { // 从表 cell = [XWDropdownMenuSubCell cellWithTableView:tableView]; cell.textLabel.font = [UIFont systemFontOfSize:14]; if ([self.dataSource respondsToSelector:@selector(dropdownMenu:subdataForRowInMainTable:)]) { NSArray *subdata = [self.dataSource dropdownMenu:self subdataForRowInMainTable:self.selectedMainRow]; cell.textLabel.text = subdata[indexPath.row]; } if ([self.dataSource respondsToSelector:@selector(dropdownMenu:subdataCountForRowInSubTable:)]) { XWBadgeView *badgeView = [[XWBadgeView alloc] initWithFrame:CGRectMake(0, 0, 0, 13)]; badgeView.backgroundColor = XWColor(205, 205, 205); badgeView.textColor = [UIColor whiteColor]; NSArray *subCount = [self.dataSource dropdownMenu:self subdataCountForRowInSubTable:self.selectedMainRow]; badgeView.font = [UIFont systemFontOfSize:12]; badgeView.badgeValue = subCount[indexPath.row]; cell.accessoryView = badgeView; } } return cell; } #pragma mark - 代理方法 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { if (tableView == self.mainTableView) { //在这里记录一下上一次选中主表的哪一行 [XWUserDefaults setObject:[NSString stringWithFormat:@"%ld",indexPath.row] forKey:kXWLastSelectedMainRow]; self.selectedMainRow = indexPath.row; [self.mainTableView reloadData]; [self.subTableView reloadData]; // 通知代理 if ([self.delegate respondsToSelector:@selector(dropdownMenu:didSelectRowInMainTable:)]) { [self.delegate dropdownMenu:self didSelectRowInMainTable:indexPath.row]; } } else { // 通知代理 if ([self.delegate respondsToSelector:@selector(dropdownMenu:didSelectRowInSubTable:inMainTable:)]) { [self.delegate dropdownMenu:self didSelectRowInSubTable:indexPath.row inMainTable:self.selectedMainRow]; } } } #pragma mark 刷新整个视图 当一个控制器中有多个XWDropdownMenu -(void)refreshMenu { [self.mainTableView reloadData]; [self.subTableView reloadData]; } -(void)setIsMainTableSmall:(BOOL)isMainTableSmall { _isMainTableSmall = isMainTableSmall; self.tableScale.constant = !isMainTableSmall ? : self.frame.size.width * kXWTableScale; } @end
运用:
#import "XWDropdownMenu.h" #define XWScreenSize [UIScreen mainScreen].bounds.size @interface ViewController ()<XWDropdownMenuDataSource,XWDropdownMenuDelegate> @property (nonatomic, strong)NSMutableArray *mainArray; @property (nonatomic, strong)NSMutableArray *subArray; @property (weak, nonatomic) XWDropdownMenu *dropdownMenu; - (IBAction)showBtnClick:(UIButton *)sender; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; [self testMenu]; } //主表数据 -(NSMutableArray *)mainArray { if (!_mainArray) { self.mainArray = [NSMutableArray array]; self.mainArray = [NSMutableArray arrayWithObjects:@"左边测试数据1",@"左边测试数据2",@"左边测试数据3",@"左边测试数据4",@"左边测试数据5",@"左边测试数据6",@"左边测试数据7",nil]; } return _mainArray; } //从表数据 -(NSMutableArray *)subArray { if (!_subArray) { self.subArray = [NSMutableArray array]; self.subArray = [NSMutableArray arrayWithObjects:@[@"右测1",@"右侧2"],@[@"右测3",@"右侧4"],@[@"右测5",@"右侧6",@"右侧7"],@[@"右测4"],@[@"右测5"],@[@"右测6"],@[@"右测7"],nil]; } return _subArray; } - (IBAction)showBtnClick:(UIButton *)sender { self.dropdownMenu.hidden = sender.selected; sender.selected = !sender.selected; } -(void)testMenu { XWDropdownMenu *menu = [XWDropdownMenu dropdownMenu]; self.dropdownMenu = menu; // menu.backgroundColor = [UIColor redColor]; menu.hidden = YES; menu.frame = CGRectMake(0, 120,XWScreenSize.width, 250); menu.dataSource = self; menu.delegate = self; menu.selectedRowTextColor = [UIColor redColor]; [self.view addSubview:menu]; } //主表行数 -(NSInteger)numberOfRowsInMainTable:(XWDropdownMenu *)dropdownMenu { return self.mainArray.count; } //主表标题 -(NSString *)dropdownMenu:(XWDropdownMenu *)dropdownMenu titleForRowInMainTable:(NSInteger)row { return self.mainArray[row]; } //子表的数据返回的是一个数组,主表每行都对应一个从表数据数组 -(NSArray *)dropdownMenu:(XWDropdownMenu *)dropdownMenu subdataForRowInMainTable:(NSInteger)row { return self.subArray[row]; } //主表行未选中图标 -(NSString *)dropdownMenu:(XWDropdownMenu *)dropdownMenu iconForRowInMainTable:(NSInteger)row { return @"collect_icon"; } //主表行选中图标 -(NSString *)dropdownMenu:(XWDropdownMenu *)dropdownMenu selectedIconForRowInMainTable:(NSInteger)row { return @"collect_icon_selected"; } //主表行的圆点提示个数 -(NSString *)dropdownMenu:(XWDropdownMenu *)dropdownMenu countForRowInMainTable:(NSInteger)row { return @"10"; } //从表行的圆点提示个数 -(NSArray *)dropdownMenu:(XWDropdownMenu *)dropdownMenu subdataCountForRowInSubTable:(NSInteger)row { return [NSArray arrayWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",@"7",nil]; } //主表选中行 -(void)dropdownMenu:(XWDropdownMenu *)dropdownMenu didSelectRowInMainTable:(NSInteger)row { NSLog(@"%@",self.mainArray[row]); } //从表选中行 -(void)dropdownMenu:(XWDropdownMenu *)dropdownMenu didSelectRowInSubTable:(NSInteger)subrow inMainTable:(NSInteger)mainRow { // self.dropdownMenu.hidden = YES; NSLog(@"%@",self.subArray[mainRow][subrow]); } @end
本实例的封装表格是左右两个列表,由于是用故事板的页面操作,所以若用纯代码可以对它进行修改,把创建表格的代码重新写入;
标签:
原文地址:http://www.cnblogs.com/wujy/p/4931703.html