自定义区头,高度自定义又要随动,就要实现自定义区头代理的同时,实现自定义区尾,只要设置为零就控制区头的高度
@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()
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)
}
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()
}
}