uitableview处理section的不悬浮,禁止section停留的方法
2016-04-11 17:55 可小猪 阅读(7782) 评论(0) 编辑 收藏 举报
//
// ViewController.m
// qwerfresa
//
// Created by mac11 on 15-1-4.
// Copyright (c) 2015年 HYL. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>{
NSArray * array;
NSArray * arrayb;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
array = [NSArrayarrayWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10", nil];
arrayb = [NSArrayarrayWithObjects:@"a",@"b",@"c",@"d",@"e",@"f",@"g",@"h",@"i",@"g", nil];
tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
tableview.delegate =self;
tableview.dataSource =self;
[self.view addSubview:tableview];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
{
return [array count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath
{
return 50;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 10;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];
view.backgroundColor = [UIColor orangeColor];
[tableView addSubview:view];
UILabel *tmpHintLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, 120, 20)];
tmpHintLabel.backgroundColor = [UIColor clearColor];
// tmpHintLabel.font = [UIFont fontWithName:TITLE_FONT size:NAV_BUTTON_SIZE];
// tmpHintLabel.textColor = [];
tmpHintLabel.text = [array objectAtIndex:section];
tmpHintLabel.textAlignment = NSTextAlignmentLeft;
[view addSubview:tmpHintLabel];
return view;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 50;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString * cellditife= @"Cell";
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellditife];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:cellditife];
}
cell.textLabel.text = [arrayb objectAtIndex:indexPath.row];
return cell;
}
//uitableview处理section的不悬浮,禁止section停留的方法,主要是这段代码
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
CGFloat sectionHeaderHeight = 50;
if(scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=0) {
scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
} else if (scrollView.contentOffset.y>=sectionHeaderHeight) {
scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end