UICollectionReusableView的用法,类似UITableView的分组

代码如下:

//注册
    [self.cv registerClass:[HybReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerView"];


-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    if (kind == UICollectionElementKindSectionHeader) {
        HybReusableView *reusableView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"headerView" forIndexPath:indexPath];
        [reusableView.moreBtn setTitle:@"更多>" forState:UIControlStateNormal];
        [reusableView.moreBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        reusableView.moreBtn.frame = CGRectMake(SWIDTH - 50, 15, 50, 20);
        NSDictionary *dict = [_array objectAtIndex:indexPath.section];
        reusableView.label.frame = CGRectMake(5, 15, 100, 20);
        if (indexPath.section == 0) {
            reusableView.label.frame = CGRectMake(5, 210, SWIDTH, 40);
            reusableView.moreBtn.frame = CGRectMake(SWIDTH - 50, 220, 50, 20);
        }
        [reusableView.label setText:[dict objectForKey:@"recname"]];
        
        reusableView.label.font = [UIFont systemFontOfSize:15];
        reusableView.moreBtn.titleLabel.font = [UIFont systemFontOfSize:15];
        [reusableView.moreBtn addTarget:self action:@selector(moreBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        return reusableView;
    }
    return nil;
}

另外需要先创建一个继承自UICollectionReusableView的类


#import <UIKit/UIKit.h>

@interface HybReusableView : UICollectionReusableView
@property (strong,nonatomic) IBOutlet UILabel *label;
@property (weak, nonatomic) IBOutlet UIButton *moreBtn;
- (void) setLabelText:(NSString *)text;
@end

#import "HybReusableView.h"

@implementation HybReusableView
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.label = [[UILabel alloc] init];
        self.moreBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        self.label.font = [UIFont systemFontOfSize:18];
        [self addSubview:self.label];
        [self addSubview:self.moreBtn];
    }
    return self;
}

- (void) setLabelText:(NSString *)text
{
    self.label.text = text;
    [self.label sizeToFit];
}
- (void)awakeFromNib {
    // Initialization code
}

@end


效果如图






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值