viewForHeaderInSection 复用问题

#import <UIKit/UIKit.h>


@interface XOSectionView : UITableViewHeaderFooterView

@property NSUInteger section;
@property (nonatomic, copy) NSString *titleName;

@property (nonatomic, weak) UITableView *tableView;

+ (CGFloat)getSectionHeight;

@end

#import "XOSectionView.h"

#define APP_SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width   // 屏幕的宽度
@interface XOSectionView()

@property (nonatomic, weak) UILabel *titleLabel;

@end

@implementation XOSectionView


-(instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier {
    
    if (self = [super initWithReuseIdentifier:reuseIdentifier]) {
        
        UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 5, APP_SCREEN_WIDTH - 20, 20)];
        titleLabel.textColor = [UIColor colorWithRed:0.35f green:0.35f blue:0.35f alpha:1.00f];
        titleLabel.font = [UIFont systemFontOfSize:15.0f];
        _titleLabel = titleLabel;
        self.backgroundColor = [UIColor whiteColor];
//        [self addSubview:titleLabel];
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 30)];
        view.backgroundColor = [UIColor whiteColor];
        [view addSubview:titleLabel];
        [self addSubview:view];
        
       
    }
    return self;
    
}

- (void)setTitleName:(NSString *)titleName {
    
    _titleLabel.text = titleName;
}

+ (CGFloat)getSectionHeight
{
    return 44;
}

- (void)setFrame:(CGRect)frame{
    //    NSLog(@"_______ frame = %@",NSStringFromCGRect(frame));
    
    CGRect sectionRect = [self.tableView rectForSection:self.section];
    CGRect newFrame = CGRectMake(CGRectGetMinX(frame), CGRectGetMinY(sectionRect), CGRectGetWidth(frame), CGRectGetHeight(frame)); [super setFrame:newFrame];
}




@end

#import "ViewController.h"
#import "SubViewController.h"
#import "XOSectionView.h"

static NSString *SectionViewID = @"XOSectionView";
@interface ViewController () <UITableViewDataSource, UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;

- (IBAction)btnClick:(UIButton *)sender;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.tableView registerClass:[XOSectionView class] forHeaderFooterViewReuseIdentifier:SectionViewID];
    
    // Do any additional setup after loading the view, typically from a nib.
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 5;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 5;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *cellName = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellName];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellName];
    }
    cell.textLabel.text = @"test";
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    

    XOSectionView *sectionView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:SectionViewID];
    
    if (sectionView) {
       
        if (section == 1) {
            sectionView.titleName = @"城市服务";
        } else {
            sectionView.titleName = @"联盟商家";
        }

        sectionView.tableView = self.tableView;
        sectionView.section = section;
    }
    
    return sectionView;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)btnClick:(UIButton *)sender {
    
    SubViewController *subVC = [[SubViewController alloc] init];
    subVC.view.backgroundColor = [UIColor whiteColor];
    [self.navigationController pushViewController:subVC animated:true];
}
@end

RecyclerView复用问题是指在列表滚动时,旧的 item 会被回收并重新用于新的 item,以提高性能和节省资源。这种复用机制可以避免频繁创建和销毁大量的 View 对象,从而减少内存占用和提高滚动的流畅性。 RecyclerView复用机制是通过 ViewHolder 模式实现的。ViewHolder 是一个包含了列表项视图的容器,它会缓存列表项中的子视图的引用,避免频繁地查找子视图。当列表项滚出屏幕时,ViewHolder 持有的子视图会被回收,然后用于新的列表项。 在 RecyclerView 中,有两个重要的方法与复用机制相关: 1. onCreateViewHolder(): 该方法用于创建 ViewHolder 对象。在该方法中,可以通过 LayoutInflater 实例化列表项的布局,并将布局传递给 ViewHolder 构造函数。 2. onBindViewHolder(): 该方法用于将数据绑定到 ViewHolder 中的视图上。在该方法中,可以根据当前位置获取对应的数据,并将数据设置给 ViewHolder 中的子视图。 通过正确使用 ViewHolder 模式,可以避免频繁创建和销毁 View 对象,提高列表的滚动性能。然而,由于复用机制的存在,也可能引发一些问题,例如数据错乱、状态混乱等。为了解决这些问题,需要在 onBindViewHolder() 方法中正确绑定数据,并在需要保存和恢复状态的情况下进行相应的处理。 总之,RecyclerView复用机制通过 ViewHolder 模式实现,可以提高列表的性能和滚动的流畅性。但在使用过程中,需要注意正确绑定数据和处理状态,以避免出现问题
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值