JXCategoryView的使用总结

一、初始化

-(JXCategoryTitleView *)categoryView{
    if (!_categoryView) {
        _categoryView = [[JXCategoryTitleView alloc] init];
        _categoryView.delegate = self;
        _categoryView.titleDataSource = self;
        _categoryView.averageCellSpacingEnabled = NO; //是否平均分配项目之间的间距
        _categoryView.contentEdgeInsetLeft = 24; //靠左显示的边距
        _categoryView.titleLabelVerticalOffset = -5; //标题向上偏移
        _categoryView.cellSpacing = 32; //固定分类项之前的间距
        _categoryView.titles = @[];
        _categoryView.defaultSelectedIndex = 0; //默认选中
        _categoryView.titleColor = RGBA(119, 119, 119, 1);           //默认文字颜色
        _categoryView.titleSelectedColor = RGBA(51, 51, 51, 1);   //文字选择颜色
        _categoryView.backgroundColor = [UIColor clearColor];
        _categoryView.titleFont = AppFont(16);
        _categoryView.titleSelectedFont = AppBoldFont(16);
        
        //底部指示器
        JXCategoryIndicatorLineView *lineView = [[JXCategoryIndicatorLineView alloc] init];
        lineView.verticalMargin = 10; //默认底部,越大越向上偏移
        lineView.indicatorHeight = 3; //指示器高度
        lineView.indicatorCornerRadius = 0; //是否倒圆角
        lineView.indicatorColor = RGBA(72, 142, 255, 1); //指示器颜色
        lineView.indicatorWidth = 24; //指示器宽度
        lineView.scrollStyle = JXCategoryIndicatorScrollStyleSameAsUserScroll; //指示器滚动样式
        _categoryView.indicators = @[lineView]; 
    
    }
    return _categoryView;
}

二、关联listContainerView

  • listContainerView 创建
-(JXCategoryListContainerView *)listContainerView{
    if (!_listContainerView) {
        _listContainerView = [[JXCategoryListContainerView alloc] initWithType:JXCategoryListContainerType_ScrollView delegate:self];
        _listContainerView.scrollView.scrollEnabled = YES;
    }
    return _listContainerView;
}
  • 与categoryView 建立关联
self.categoryView.listContainer = self.listContainerView;
  • 实现listContainerView 代理

//子控制器数组
- (NSArray<__kindof UIViewController *> *)controllers{
    return @[
        self.VC1,
        self.VC2,
    ];
}



#pragma mark - JXCategoryListContainerViewDelegate -
- (id<JXCategoryListContentViewDelegate>)listContainerView:(JXCategoryListContainerView *)listContainerView initListForIndex:(NSInteger)index{
    __kindof UIViewController *vc  = self.controllers[index];
    return vc;
}
- (NSInteger)numberOfListsInlistContainerView:(JXCategoryListContainerView *)listContainerView {
    return self.controllers.count;
}

//定义scrollerview处理手势冲突
- (Class)scrollViewClassInlistContainerView:(JXCategoryListContainerView *)listContainerView{
    return [ServiceScrollView class];
}

三、titles 重载

可以在初始化时指定titles属性赋值, 如果需要动态显示titles, 则可以在处理后,通过 reloadData 进行重载

  • 初始化时指定
_categoryView.titles = @[@"项目1",@"项目2"];
  • 动态处理
NSArray *titles;
if (xxx) {
	titles = @[@"项目1",@"项目2"];
}else {
	titles = @[@"礼物1",@"礼物2"];
}
_categoryView.titles = titles;
[_categoryView reloadData];

四、设置指定项被选中

[self.categoryView selectItemAtIndex:0];

五、代理方法

#pragma mark - JXCategoryViewDelegate -

//点击选中的情况才会调用该方法
- (void)categoryView:(JXCategoryBaseView *)categoryView didClickSelectedItemAtIndex:(NSInteger)index {
    
}

六、listContentView 需实现 listView 方法


@protocol JXCategoryListContentViewDelegate <NSObject>

/**
 如果列表是VC,就返回VC.view
 如果列表是View,就返回View自己

 @return 返回列表视图
 */
- (UIView *)listView;

@optional

/**
 可选实现,列表将要显示的时候调用
 */
- (void)listWillAppear;

/**
 可选实现,列表显示的时候调用
 */
- (void)listDidAppear;

/**
 可选实现,列表将要消失的时候调用
 */
- (void)listWillDisappear;

.....

根据协议声明来看, listView方法需要协议的实现者必须 实现才可以。 因为它是 @required (不指定,则为默认)声明的

#pragma mark - JXCategoryListContainerViewDelegate -
- (id<JXCategoryListContentViewDelegate>)listContainerView:(JXCategoryListContainerView *)listContainerView initListForIndex:(NSInteger)index{
    __kindof UIViewController *vc  = self.controllers[index];
    return vc;
}

- (id<JXCategoryListContentViewDelegate>)listContainerView:(JXCategoryListContainerView *)listContainerView initListForIndex:(NSInteger)index 这个回调需要返回实现了 JXCategoryListContentViewDelegate 的对象(一般是viewController)

@implementation MyContentViewController 

//实现 JXCategoryListContentViewDelegate 的代理方法
- (UIView *)listView{
    return self.view;
}

@end

热门相关:我能看到隐藏机缘   风流医圣   学霸,你女朋友掉了   全民女神之重生腹黑千金   血战天下