JXCategoryView的使用總結

reyzhang發表於2024-06-25

一、初始化

-(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