UITableView自定义区头

自定义区头,高度自定义又要随动,就要实现自定义区头代理的同时,实现自定义区尾,只要设置为零就控制区头的高度
@interface AnniversayDayViewController ()<UITableViewDataSource,UITableViewDelegate>


@property (nonatomic, strong)UITableView *tableView;

@property (nonatomic, strong)NSMutableArray *dataArray;

@end

@implementation AnniversayDayViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setNavigationBarPinkWithTitle:@"纪念日"];
    self.dataArray = [NSString getAllHolidaiesFromFuture];
    [self tableView];
 }
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    switch (section) {
        case 0:
            return 1;
            break;
        case 1:
            return self.dataArray.count;
            break;
        default:
            break;
    }
    return 0;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        Anniversay_TwoCell *cell = [tableView dequeueReusableCellWithIdentifier:kAnniversay_TwoCellId];
        return cell;
    }else{
        Anniversay_OneCell *cell = [tableView dequeueReusableCellWithIdentifier:kAnniversay_OneCellId];
        if (indexPath.row == 0) {
            [cell suitCellWithTopSep:YES withDataStr:self.dataArray[indexPath.row]];
        }else{
            [cell suitCellWithTopSep:NO withDataStr:self.dataArray[indexPath.row]];
        }
        return cell;
    }
    return nil;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return kAnniversay_OneCellHeight;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        AddRemindViewController *addVc = [[AddRemindViewController alloc]init];
        [self.navigationController pushViewController:addVc animated:YES];
    }
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"reuse"];
    UILabel *titleLabel = [header viewWithTag:1000];
    if (!header) {
        header = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kSCREEN_WIDTH, 0*kAUTOLAYOUTSCALE)];
        header.backgroundColor = [UIColor cyanColor];
        titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(15*kAUTOLAYOUTSCALE, 5*kAUTOLAYOUTSCALE, kSCREEN_WIDTH, 30*kAUTOLAYOUTSCALE)];
        titleLabel.tag = 1000;
        titleLabel.backgroundColor = [UIColor cyanColor];
        [header addSubview:titleLabel];
    }
    NSString *title = @"近日待办";
    titleLabel.top = 5*kAUTOLAYOUTSCALE;
    if (section == 1) {
        title = @"节假日";
        titleLabel.top = 15*kAUTOLAYOUTSCALE;
    }
    titleLabel.text = title;
    return header;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    switch (section) {
        case 0:
            return 35*kAUTOLAYOUTSCALE;
            break;
        case 1:
            return 45*kAUTOLAYOUTSCALE;
            break;
        default:
            break;
    }
    return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 1;这是控制区头高度的关键点
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *view=[[UIView alloc] initWithFrame:CGRectZero];///同样需要设置
    return view;
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

-(UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kSCREEN_WIDTH, kSCREEN_HEIGHT-kNavbarAndStatusHieght) style:UITableViewStyleGrouped];/控制随动/悬浮的属性
        [_tableView registerNib:[UINib nibWithNibName:@"Anniversay_OneCell" bundle:nil] forCellReuseIdentifier:kAnniversay_OneCellId];
        [_tableView registerNib:[UINib nibWithNibName:@"Anniversay_TwoCell" bundle:nil] forCellReuseIdentifier:kAnniversay_TwoCellId];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = kColorBg;
        [self.view addSubview:_tableView];
     }
    return _tableView;
}


@end
SWIFT版本
import UIKit

class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {

    var tableView:UITableView?
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        addTableView()
    }

    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 4
    }
    func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let header = UIView(frame: CGRectMake(0,0,300,25))
        header.backgroundColor = UIColor.cyanColor()
        return header
    }
    func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 25
    }
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 20
    }
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        var cell = tableView.dequeueReusableCellWithIdentifier("reuse")
        if cell == nil{
         cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "reuse")
        }
        cell?.textLabel?.text = NSString(format: "%d", indexPath.row) as String
        return cell!
    }
    func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 1这是控制区头高度的关键点
    }
    func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        return UIView(frame: CGRectZero)同时也要设置frame为0
    }
    func addTableView(){
    tableView = UITableView(frame: CGRectMake(0, 0, self.view.frame.width, self.view.frame.height), style: UITableViewStyle.Grouped)
        tableView?.delegate = self
        tableView?.dataSource = self
        self.view.addSubview(tableView!)

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值