Section開合效果及header拉伸效果

使用到的全局變量:

@implementation SpringTableViewHeader
{
    UITableView *myTableView;
    UIImageView *bannerImg;//header圖片
    NSMutableArray *sectionArr;//記錄section開合狀態(tài)的數(shù)組
}

sectionArr = [NSMutableArray arrayWithObjects:@"1",@"0",@"0", nil];//"1"表示展開,"0"表示收起

創(chuàng)建一個TableView和它的header:

- (void)createView {
    myTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, -kScaleY*200, UI_SCREEN_WIDTH, UI_SCREEN_HEIGHT+kScaleY*200) style:UITableViewStylePlain];
    myTableView.delegate = self;
    myTableView.dataSource = self;
    myTableView.tableHeaderView = [self createHeaderView];
    myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:myTableView];
    
    //返回
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    backBtn.frame = CGRectMake(6, 25, 37, 34);
    [backBtn addTarget:self action:@selector(backPre) forControlEvents:UIControlEventTouchUpInside];
    [backBtn setImage:[UIImage imageNamed:@"返回"] forState:UIControlStateNormal];
    [self.view addSubview:backBtn];
}

- (UIView *)createHeaderView {
    UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, UI_SCREEN_WIDTH, kScaleY*500)];
    headerView.clipsToBounds = YES;
    
    bannerImg = [[UIImageView alloc]initWithFrame:headerView.frame];
    bannerImg.image = [UIImage imageNamed:@"swim"];
    bannerImg.contentMode = UIViewContentModeScaleAspectFill;
    [headerView addSubview:bannerImg];
    
    return headerView;
}
#pragma mark - tableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return sectionArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if ([sectionArr[section] isEqualToString:@"1"]) {
        return 4;
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identifier = @"springCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [self setCell:cell withIndexPath:indexPath];
    
    return cell;
}

- (void)setCell:(UITableViewCell *)cell withIndexPath:(NSIndexPath *)indexPath {
    [cell.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    UILabel *cellLabel = [[UILabel alloc]init];
    cellLabel.text = [NSString stringWithFormat:@"第%ld行",indexPath.row];
    cellLabel.textColor = [UIColor colorWithHexString:@"#333333" alpha:1];
    cellLabel.font = [UIFont fontWithName:LightFont size:17];
    [cellLabel sizeToFit];
    cellLabel.center = CGPointMake(kScaleX*15+cellLabel.frame.size.width/2, kScaleY*50/2);
    [cell.contentView addSubview:cellLabel];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return kScaleY*50;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
 
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *sectionHeader = [[UIView alloc]initWithFrame:CGRectMake(0, 0, UI_SCREEN_WIDTH, kScaleY*65)];
    sectionHeader.backgroundColor = [UIColor whiteColor];
    UITapGestureRecognizer *sectionHeaderTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(sectionHeaderAction:)];
    sectionHeader.tag = section;
    [sectionHeader addGestureRecognizer:sectionHeaderTap];
    
    UILabel *sectionTitle = [[UILabel alloc]init];
    sectionTitle.text = [Utils iphoneType];
    sectionTitle.textColor = [UIColor colorWithHexString:@"#333333" alpha:1];
    sectionTitle.font = [UIFont fontWithName:LightFont size:15];
    [sectionTitle sizeToFit];
    sectionTitle.center = CGPointMake(kScaleX*15+sectionTitle.frame.size.width/2, kScaleY*65/2);
    [sectionHeader addSubview:sectionTitle];
    
    UIView *sectionLine = [[UIView alloc]initWithFrame:CGRectMake(kScaleX*15, kScaleY*65-1, kScaleX*360, 1)];
    sectionLine.backgroundColor = [UIColor colorWithHexString:@"#d8d8d8" alpha:1];
    [sectionHeader addSubview:sectionLine];
    
    return sectionHeader;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return kScaleY*65;
}

點擊section的開合效果:

- (void)sectionHeaderAction:(UITapGestureRecognizer *)tap {
    if ([sectionArr[tap.view.tag] isEqualToString:@"1"]) {
        [sectionArr replaceObjectAtIndex:tap.view.tag withObject:@"0"];
    }else {
        [sectionArr replaceObjectAtIndex:tap.view.tag withObject:@"1"];
    }
    [myTableView reloadSections:[NSIndexSet indexSetWithIndex:tap.view.tag] withRowAnimation:UITableViewRowAnimationNone];
}

header圖片拉伸效果:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat downHight = -scrollView.contentOffset.y;//下拉的距離
    CGRect frame = bannerImg.frame;
    if (downHight < 0) {
        frame.size.height = kScaleY*500;
        bannerImg.frame = frame;
    }else if (downHight < kScaleY*200) {
        frame.size.height = kScaleY*500 + downHight;
        bannerImg.frame = frame;
    }else {
        frame.size.height = kScaleY*500 + kScaleY*200;
        bannerImg.frame = frame;
        
        myTableView.contentOffset = CGPointMake(0, -kScaleY*200);//最多往下拉動的距離家妆,防止漏出白邊
    }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末旁仿,一起剝皮案震驚了整個濱河市砖织,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌拇舀,老刑警劉巖囊嘉,帶你破解...
    沈念sama閱讀 221,273評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件捌显,死亡現(xiàn)場離奇詭異俱箱,居然都是意外死亡,警方通過查閱死者的電腦和手機些椒,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,349評論 3 398
  • 文/潘曉璐 我一進店門播瞳,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人免糕,你說我怎么就攤上這事赢乓。” “怎么了石窑?”我有些...
    開封第一講書人閱讀 167,709評論 0 360
  • 文/不壞的土叔 我叫張陵牌芋,是天一觀的道長。 經(jīng)常有香客問我松逊,道長躺屁,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,520評論 1 296
  • 正文 為了忘掉前任经宏,我火速辦了婚禮犀暑,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘烁兰。我一直安慰自己耐亏,他們只是感情好,可當我...
    茶點故事閱讀 68,515評論 6 397
  • 文/花漫 我一把揭開白布缚柏。 她就那樣靜靜地躺著苹熏,像睡著了一般碟贾。 火紅的嫁衣襯著肌膚如雪币喧。 梳的紋絲不亂的頭發(fā)上轨域,一...
    開封第一講書人閱讀 52,158評論 1 308
  • 那天,我揣著相機與錄音杀餐,去河邊找鬼干发。 笑死,一個胖子當著我的面吹牛史翘,可吹牛的內(nèi)容都是我干的枉长。 我是一名探鬼主播,決...
    沈念sama閱讀 40,755評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼琼讽,長吁一口氣:“原來是場噩夢啊……” “哼必峰!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起钻蹬,我...
    開封第一講書人閱讀 39,660評論 0 276
  • 序言:老撾萬榮一對情侶失蹤吼蚁,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后问欠,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體肝匆,經(jīng)...
    沈念sama閱讀 46,203評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,287評論 3 340
  • 正文 我和宋清朗相戀三年顺献,在試婚紗的時候發(fā)現(xiàn)自己被綠了旗国。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,427評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡注整,死狀恐怖能曾,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情肿轨,我是刑警寧澤借浊,帶...
    沈念sama閱讀 36,122評論 5 349
  • 正文 年R本政府宣布,位于F島的核電站萝招,受9級特大地震影響蚂斤,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜槐沼,卻給世界環(huán)境...
    茶點故事閱讀 41,801評論 3 333
  • 文/蒙蒙 一曙蒸、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧岗钩,春花似錦纽窟、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,272評論 0 23
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春审孽,著一層夾襖步出監(jiān)牢的瞬間县袱,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,393評論 1 272
  • 我被黑心中介騙來泰國打工佑力, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留式散,地道東北人。 一個月前我還...
    沈念sama閱讀 48,808評論 3 376
  • 正文 我出身青樓打颤,卻偏偏與公主長得像暴拄,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子编饺,可洞房花燭夜當晚...
    茶點故事閱讀 45,440評論 2 359

推薦閱讀更多精彩內(nèi)容