SegmentControl自定义cell

ios15下样式如图 其他低版本样式有待测试


te1.png

h文件

#import 

NS_ASSUME_NONNULL_BEGIN

@interface SF_SegmentCommonTableViewCell : UITableViewCell
@property(nonatomic, strong) NSArray * itemArray;
@property(nonatomic, strong) NSString * ite;
@property(nonatomic, copy) void(^segChannge)(NSInteger idx);
/*
 *  设置segmentmentControl的item
 */
- (void)setSegmentItemArray:(NSArray *)itemArray;
@end

NS_ASSUME_NONNULL_END

m文件

#import "SF_SegmentCommonTableViewCell.h"
@interface SF_SegmentCommonTableViewCell()
@property(nonatomic, strong) QMUISegmentedControl * segmentControl;

@end
@implementation SF_SegmentCommonTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        [self.contentView addSubview:self.segmentControl];
        
        [self.segmentControl setBackgroundWithNormalImage:[UIImage imageWithColor:HexRGB(0xffffff) size:CGSizeMake(150, 40)] selectedImage:[UIImage imageWithColor:HexRGB(0x1684fc) size:CGSizeMake(150, 40)] devideImage00:nil devideImage01:nil devideImage10:nil textColor:HexRGB(0x1684fc) selectedTextColor:HexRGB(0xffffff) fontSize:[UIFont systemFontOfSize:14]];
        self.segmentControl.layer.borderColor = HexRGB(0x1684fc).CGColor;
        self.segmentControl.layer.borderWidth = 1;
        
        [self.segmentControl addTarget:self action:@selector(change:) forControlEvents:UIControlEventValueChanged];

    }
    return self;
}
//点击不同分段就会有不同的事件进行相应
-(void)change:(UISegmentedControl *)sender{
    BLOCK_EXEC(self.segChannge,sender.selectedSegmentIndex);
}
- (void)setSegmentItemArray:(NSArray *)itemArray{
    
    if (itemArray.count > 0) { //确保insert一次
        
        [self.segmentControl removeAllSegments];
        [itemArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                [self.segmentControl insertSegmentWithTitle:obj atIndex:idx animated:NO];
        }];
    }
    self.segmentControl.selectedSegmentIndex = 0;

}


- (void)layoutSubviews
{
    [super layoutSubviews];
    [self.segmentControl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).offset(50);
        make.right.equalTo(self.contentView).offset(-50);
        make.top.equalTo(self.contentView).offset(5);
        make.bottom.equalTo(self.contentView).offset(-5);
    }];
}
- (QMUISegmentedControl *)segmentControl{
    if (!_segmentControl) {
        _segmentControl = [[QMUISegmentedControl alloc] init];
    }
    return _segmentControl;
}


@end

使用的qmui的组件为父类
pod导入

platform :ios, '10.0'
source 'https://github.com/CocoaPods/Specs.git'
use_frameworks!
target :yourName do
   pod 'QMUIKit/QMUIComponents/QMUISegmentedControl'
end

设计的纯色image方法是yykit里的方法

+ (UIImage *)imageWithColor:(UIColor *)color size:(CGSize)size {
    if (!color || size.width <= 0 || size.height <= 0) return nil;
    CGRect rect = CGRectMake(0.0f, 0.0f, size.width, size.height);
    UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, color.CGColor);
    CGContextFillRect(context, rect);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

自定义的cell使用方法

- (SF_SegmentCommonTableViewCell *)segCell{
    NSString *cellId = @"segCell";
    _segCell = [self.tableView dequeueReusableCellWithIdentifier:cellId];
    if (!_segCell) {
        _segCell = [[SF_SegmentCommonTableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:cellId];
    }
    [_segCell setSegmentItemArray:@[@"梁博",@"Fox",@"雷霆嘎巴"]];
    _segCell.segChannge = ^(NSInteger idx) {
        NSLog(@"滑动到了%ld",idx);
        
    };
    return _segCell;
}

作为cell或者header直接return就行

return self.segCell

你可能感兴趣的:(SegmentControl自定义cell)