美团HD(4)-二级联动效果

DJNavDropView.m

复制代码
#import "DJNavDropView.h"
#import "DJCategory.h"
#import "DJNavMainCategoryCell.h"
#import "DJNavSubCategoryCell.h"


@interface DJNavDropView()<UITableViewDataSource,UITableViewDelegate>

/** 主分类 */
@property (weak, nonatomic) IBOutlet UITableView *mainTableView;
/** 子分类 */
@property (weak, nonatomic) IBOutlet UITableView *subTableView;
/** 选中的子类别集合 */
@property (nonatomic,strong) NSArray *selectedSubCategories;


@end



@implementation DJNavDropView

+ (instancetype)dropView {

   return[[[NSBundle mainBundle] loadNibNamed:@"DJNavDropView" owner:nil options:nil] lastObject];

}

- (void)setCategoryList:(NSArray *)categoryList {

    _categoryList = categoryList;
    
    // 刷新数据
    [self.mainTableView reloadData];

}


#pragma mark - TableView 数据源方法

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    if (tableView == self.mainTableView) { // 主类别
        return self.categoryList.count;
    } else { // 子类别
        return self.selectedSubCategories.count;
    }
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    if (tableView == self.mainTableView) { // 主类别
        
        DJNavMainCategoryCell *cell = [DJNavMainCategoryCell cellWithTableView:tableView];
        // 设置当前Cell属性
        DJCategory *categoryItem = self.categoryList[indexPath.row];
        cell.textLabel.text = categoryItem.name;
        cell.imageView.image = [UIImage imageNamed:categoryItem.icon];
        // 如果当前主类别有子类别
        if (categoryItem.subcategories.count) {
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; // 显示向右的箭头
        } else {
            cell.accessoryType = UITableViewCellAccessoryNone; // 隐藏箭头
        }
        return cell;
        
    } else { // 子类别
    
        DJNavSubCategoryCell *cell = [DJNavSubCategoryCell cellWithTableView:tableView];
        // 设置当前Cell属性
        cell.textLabel.text = self.selectedSubCategories[indexPath.row];
        return cell;
        
    }
}



#pragma mark - TableView 代理方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    if (tableView == self.mainTableView) { // 点击主分类上面的条目
        DJCategory *category = self.categoryList[indexPath.row];
        self.selectedSubCategories = category.subcategories;
        // 刷新子栏目列表数据
        [self.subTableView reloadData];
    } else { // 点击子分类上面的条目
        
    }

}



@end
复制代码

DJNavMainCategoryCell.m

复制代码
#import "DJNavMainCategoryCell.h"

@implementation DJNavMainCategoryCell


+ (instancetype)cellWithTableView:(UITableView *)tableView {

    static NSString *ID = @"main_category";
    DJNavMainCategoryCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[DJNavMainCategoryCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
    }
    return cell;
}


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {

    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
       UIImageView *bg = [[UIImageView alloc] init];
        bg.image = [UIImage imageNamed:@"bg_dropdown_leftpart"];
        self.backgroundView = bg;
        
        UIImageView *selectedBg = [[UIImageView alloc] init];
        selectedBg.image = [UIImage imageNamed:@"bg_dropdown_left_selected"];
        self.selectedBackgroundView = selectedBg;
        
    }
    return self;
}

@end
复制代码

最终效果:



如果您觉得阅读本文对您有帮助,请点一下“推荐”按钮,您的“推荐”将是我最大的写作动力!欢迎各位转载,但是未经作者本人同意,转载文章之后必须在文章页面明显位置给出作者和原文连接,否则保留追究法律责任的权利。
posted @   夜行过客  阅读(396)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
点击右上角即可分享
微信分享提示