iOS_三级分类列表实现

1.效果图如下:

iOS_三级分类列表实现_第1张图片
全部分类.png
直接上代码了哈,其实是3个tableView封装在View里面,看代码就明白了.

view.h

#import "WYSBaseView.h"
#import 
#define WSNoFound (-1)
@interface WSIndexPath : NSObject
@property (nonatomic,assign) NSInteger column; //区分  0 为左边的   1 是 右边的
@property (nonatomic,assign) NSInteger row; //左边第一级的行
@property (nonatomic,assign) NSInteger item; //左边第二级的行
@property (nonatomic,assign) NSInteger rank; //左边第三级的行

+ (instancetype)twIndexPathWithColumn:(NSInteger )column
                                  row:(NSInteger )row
                                 item:(NSInteger )item
                                 rank:(NSInteger )rank;

@end
@class WYSHomeLookOverAllClassView;
@protocol WYSHomeLookOverAllClassViewDataSource 
- (NSInteger )dropMenuView:(WYSHomeLookOverAllClassView *)dropMenuView numberWithIndexPath:(WSIndexPath *)indexPath;
- (NSString *)dropMenuView:(WYSHomeLookOverAllClassView *)dropMenuView titleWithIndexPath:(WSIndexPath *)indexPath;
@end
@protocol WYSHomeLookOverAllClassViewDelegate 
- (void)dropMenuView:(WYSHomeLookOverAllClassView *)dropMenuView didSelectWithIndexPath:(WSIndexPath *)indexPath typeName:(NSString *)typeName;
- (void)selectWhichFirstTypeName:(NSString *)typeName;
- (void)selectWhichSecondTypeName:(NSString *)typeName;
@end
@interface WYSHomeLookOverAllClassView : WYSBaseView
@property (nonatomic,weak) id dataSource;
@property (nonatomic,weak) id delegate;
- (void)showTableView;
@end

view.m

#import "WYSHomeLookOverAllClassView.h"
@implementation WSIndexPath
+ (instancetype)twIndexPathWithColumn:(NSInteger)column
                                  row:(NSInteger)row
                                 item:(NSInteger)item
                                 rank:(NSInteger)rank{
    WSIndexPath *indexPath = [[self alloc] initWithColumn:column row:row item:item rank:rank];
    return indexPath;
}
- (instancetype)initWithColumn:(NSInteger )column
                           row:(NSInteger )row
                          item:(NSInteger )item
                          rank:(NSInteger )rank{
    
    if (self = [super init]) {
        
        self.column = column;
        self.row = row;
        self.item = item;
        self.rank = rank;
        
    }
    
    return self;
}


@end

static NSString *cellIdent = @"cellIdent";

@interface WYSHomeLookOverAllClassView ()
{
    NSInteger _currSelectColumn;
    NSInteger _currSelectRow;
    NSInteger _currSelectItem;
    NSInteger _currSelectRank;
    
    CGFloat _rightHeight;
    BOOL _isLeftOpen;
}

@property (nonatomic,strong) UITableView *leftTableView;
@property (nonatomic,strong) UITableView *leftTableView_1;
@property (nonatomic,strong) UITableView *leftTableView_2;

@end

@implementation WYSHomeLookOverAllClassView


-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    
    if (self) {
        [self _initialize];
        [self _setSubViews];
    }
    return self;
}
- (void)_initialize{
    
    _currSelectColumn = 0;
    _currSelectItem = WSNoFound;
    _currSelectRank = WSNoFound;
    _currSelectRow = WSNoFound;
    _isLeftOpen = NO;
}
#pragma mark -- 添加tableView
- (void)_setSubViews{
    [self addSubview:self.leftTableView];
    [self addSubview:self.leftTableView_1];
    [self addSubview:self.leftTableView_2];
}
#pragma mark -- 懒加载tableView
- (UITableView *)leftTableView{
    
    if (!_leftTableView) {
        
        _leftTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _leftTableView.delegate = self;
        _leftTableView.dataSource = self;
        [_leftTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:cellIdent];
        _leftTableView.frame = CGRectMake(0, 0, WYSScreenWidth/3.0, 0);
        _leftTableView.tableFooterView = [[UIView alloc]init];
    }
    
    return _leftTableView;
}

- (UITableView *)leftTableView_1{
    if (!_leftTableView_1) {
        _leftTableView_1 = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _leftTableView_1.delegate = self;
        _leftTableView_1.dataSource = self;
        [_leftTableView_1 registerClass:[UITableViewCell class] forCellReuseIdentifier:cellIdent];
        _leftTableView_1.frame = CGRectMake( WYSScreenWidth/3.0, 0 , WYSScreenWidth/3.0, 0);
        _leftTableView_1.backgroundColor = [UIColor colorWithRed:230/255.0 green:230/255.0 blue:230/255.0 alpha:1.0];
        _leftTableView_1.tableFooterView = [[UIView alloc]init];
    }
    return _leftTableView_1;
}
- (UITableView *)leftTableView_2{
    if (!_leftTableView_2) {
        
        _leftTableView_2 = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _leftTableView_2.delegate = self;
        _leftTableView_2.dataSource = self;
        [_leftTableView_2 registerClass:[UITableViewCell class] forCellReuseIdentifier:cellIdent];
        _leftTableView_2.frame = CGRectMake( WYSScreenWidth/3.0 * 2, 0 , WYSScreenWidth/3.0, 0);
        _leftTableView_2.backgroundColor = [UIColor colorWithRed:243/255.0 green:243/255.0 blue:243/255.0 alpha:1.0];
        _leftTableView_2.tableFooterView = [[UIView alloc]init];
        
    }
    return _leftTableView_2;
    
}
- (void)_showLeftTableViews{
    
    self.leftTableView.frame = CGRectMake(self.leftTableView.frame.origin.x, self.leftTableView.frame.origin.y, self.leftTableView.frame.size.width, WYSScreenHeight);
    self.leftTableView_1.frame = CGRectMake(self.leftTableView_1.frame.origin.x, self.leftTableView_1.frame.origin.y, self.leftTableView_1.frame.size.width, WYSScreenHeight);
    self.leftTableView_2.frame = CGRectMake(self.leftTableView_2.frame.origin.x, self.leftTableView_2.frame.origin.y, self.leftTableView_2.frame.size.width, WYSScreenHeight);
}
#pragma mark -- 显示tableView;
- (void)showTableView{
    
    _currSelectColumn = 0;
    _isLeftOpen = YES;
    self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, WYSScreenWidth, WYSScreenHeight);
    
    [UIView animateWithDuration:0.2 animations:^{
        
        if (_currSelectColumn == 0) {
            [self _showLeftTableViews];
        }
        
    } completion:^(BOOL finished) {
        
    }];
}
#pragma mark -- DataSource -
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    WSIndexPath *twIndexPath =[self _getTwIndexPathForNumWithtableView:tableView];
    if (self.dataSource && [self.dataSource respondsToSelector:@selector(dropMenuView:numberWithIndexPath:)]) {
        
        NSInteger count =  [self.dataSource dropMenuView:self numberWithIndexPath:twIndexPath];
    
        if (twIndexPath.column == 1) {
            _rightHeight = count * 44.0;
        }
        return count;
    }else{
        return 0;
    }
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    WSIndexPath *twIndexPath = [self _getTwIndexPathForCellWithTableView:tableView indexPath:indexPath];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdent];
    cell.selectedBackgroundView = [[UIView alloc] init];
    cell.selectedBackgroundView.backgroundColor =  [UIColor colorWithRed:233/255.0 green:233/255.0 blue:233/255.0 alpha:1.0];
    cell.textLabel.textColor = [UIColor colorWithWhite:0.004 alpha:1.000];
    cell.textLabel.font = [UIFont systemFontOfSize:13];
    cell.backgroundColor = [UIColor clearColor];
    cell.textLabel.highlightedTextColor = [UIColor blackColor];
    //    [UIColor colorWithRed:233/255.0 green:233/255.0 blue:233/255.0 alpha:1.0];
    if (self.dataSource && [self.dataSource respondsToSelector:@selector(dropMenuView:titleWithIndexPath:)]) {
        
        cell.textLabel.text =  [self.dataSource dropMenuView:self titleWithIndexPath:twIndexPath];
    }else{
        
        cell.textLabel.text = [NSString stringWithFormat:@"%ld",(long)indexPath.row];
    }
    
    
    return cell;
    
}


#pragma mark - tableView delegate -
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    WSIndexPath *twIndexPath = [self _getTwIndexPathForCellWithTableView:tableView indexPath:indexPath];

    if (tableView == self.leftTableView) {
        UITableViewCell *cellF = [_leftTableView cellForRowAtIndexPath:indexPath];
        _currSelectRow = indexPath.row;
        _currSelectItem = WSNoFound;
        _currSelectRank = WSNoFound;
        NSString *firstTypeName = [NSString stringWithFormat:@"%@", cellF.textLabel.text];
        [self.delegate selectWhichFirstTypeName:firstTypeName];
        [self.leftTableView_1 reloadData];
        [self.leftTableView_2 reloadData];
    }
    if (tableView == self.leftTableView_1) {
        UITableViewCell *cellS = [_leftTableView_1 cellForRowAtIndexPath:indexPath];
        _currSelectRank = WSNoFound;
        _currSelectItem = indexPath.row;
        NSString *secondTypeName = [NSString stringWithFormat:@"%@", cellS.textLabel.text];
        [self.delegate selectWhichSecondTypeName:secondTypeName];
        [self.leftTableView_2 reloadData];
    }
    if (self.leftTableView_2 == tableView) {
        UITableViewCell *cellT = [_leftTableView_2 cellForRowAtIndexPath:indexPath];
        [self.delegate dropMenuView:self didSelectWithIndexPath:twIndexPath typeName:cellT.textLabel.text];
    }
}
- (WSIndexPath *)_getTwIndexPathForNumWithtableView:(UITableView *)tableView{
    if (tableView == self.leftTableView) {
        return  [WSIndexPath twIndexPathWithColumn:_currSelectColumn row:WSNoFound item:WSNoFound rank:WSNoFound];
    }
    if (tableView == self.leftTableView_1 && _currSelectRow != WSNoFound) {
        return [WSIndexPath twIndexPathWithColumn:_currSelectColumn row:_currSelectRow item:WSNoFound rank:WSNoFound];
    }
    if (tableView == self.leftTableView_2 && _currSelectRow != WSNoFound && _currSelectItem != WSNoFound) {
        return [WSIndexPath twIndexPathWithColumn:_currSelectColumn row:_currSelectRow item:_currSelectItem  rank:WSNoFound];
    }
    return  0;
}

- (WSIndexPath *)_getTwIndexPathForCellWithTableView:(UITableView *)tableView indexPath:(NSIndexPath *)indexPath{
    if (tableView == self.leftTableView) {
        return  [WSIndexPath twIndexPathWithColumn:0 row:indexPath.row item:WSNoFound rank:WSNoFound];
    }
    if (tableView == self.leftTableView_1) {
        return [WSIndexPath twIndexPathWithColumn:_currSelectColumn row:_currSelectRow item:indexPath.row rank:WSNoFound];
    }
    
    if (tableView == self.leftTableView_2) {
        return [WSIndexPath twIndexPathWithColumn:_currSelectColumn row:_currSelectRow item:_currSelectItem  rank:indexPath.row];
    }
    
    
    return  [WSIndexPath twIndexPathWithColumn:0 row:indexPath.row item:WSNoFound rank:WSNoFound];
    
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    
    if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
        [cell setPreservesSuperviewLayoutMargins:NO];
    }
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}
@end

使用的话就是在VC里面签订协议方法,并实现,将数据显示出来就可以了!!! 主要就是下面这2个协议方法,将数据源换成你自己的就可以了:

#pragma mark - WYSHomeLookOverAllClassView DataSource -
- (NSInteger)dropMenuView:(WYSHomeLookOverAllClassView *)dropMenuView numberWithIndexPath:(WSIndexPath *)indexPath{
    
    //WSIndexPath 类里面有注释
    
    if (indexPath.column == 0 && indexPath.row == WSNoFound) {
        
        return 1;
    }
    if (indexPath.column == 0 && indexPath.row != WSNoFound && indexPath.item == WSNoFound) {
        
        return 2;
    }
    
    if (indexPath.column == 0 && indexPath.row != WSNoFound && indexPath.item != WSNoFound && indexPath.rank == WSNoFound) {
        return 3;
    }
    
    return 0;
}

- (NSString *)dropMenuView:(WYSHomeLookOverAllClassView *)dropMenuView titleWithIndexPath:(WSIndexPath *)indexPath{
    
    //左边 第一级
    if (indexPath.column == 0 && indexPath.row != WSNoFound && indexPath.item == WSNoFound) 
        return [NSString stringWithFormat:@"%@",modeFir.type_name];
    }
    
    if (indexPath.column == 0 && indexPath.row != WSNoFound && indexPath.item != WSNoFound && indexPath.rank == WSNoFound) {
        return [NSString stringWithFormat:@"%@",modelSec.type_name];
    }
    
    if (indexPath.column == 0 && indexPath.row != WSNoFound && indexPath.item != WSNoFound && indexPath.rank != WSNoFound) {
        return [NSString stringWithFormat:@"%@",modelThree.type_name];
    }
    return @"";
    
}

如果不明白:
Demo地址
记得star;

你可能感兴趣的:(iOS_三级分类列表实现)