viewForHeaderInSection 复用问题

本文深入探讨了iOS开发中的Swift编程语言,分享了实际项目中使用的高效编码实践和最佳编程习惯,帮助开发者提高开发效率并创建高质量的应用程序。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值