码迷,mamicode.com
首页 > 其他好文 > 详细

uitableview处理section的不悬浮,禁止section停留的方法

时间:2016-04-11 18:28:11      阅读:139      评论:0      收藏: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

uitableview处理section的不悬浮,禁止section停留的方法

标签:

原文地址:http://www.cnblogs.com/kexiaozhu/p/5379386.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!