自定义UITableView折叠效果

类似于QQ的那种折叠效果。只刷新点击的折叠行。不加载所有数据源。 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
//
//  MyTableViewController.m
//  TableSectionStatistics
//
//  Created by rlf on 12-11-23.
//  Copyright (c) 2012年 __rlf__. All rights reserved.
//
 
#import "MyTableViewController.h"
#define originalHeight 25.0f
#define newHeight 85.0f
#define isOpen @"85.0f"
 
@interface MyTableViewController ()
 
@end
 
@implementation MyTableViewController
{
     NSMutableDictionary *dicClicked;
     NSInteger count;
     CGFloat mHeight;
     NSInteger sectionIndex;
}
 
- ( id )initWithStyle:(UITableViewStyle)style
{
     self = [ super initWithStyle :style];
     if ( self ) {
         // Custom initialization
     }
     return self ;
}
 
- ( void )viewDidLoad
{
     [ super viewDidLoad ];
     count = 0 ;
     mHeight = originalHeight;
     sectionIndex = 0 ;
     dicClicked = [ NSMutableDictionary dictionaryWithCapacity : 3 ];
}
 
- ( void )viewDidUnload
{
     [ super viewDidUnload ];
     // Release any retained subviews of the main view.
     // e.g. self.myOutlet = nil;
}
 
#pragma mark - Table view data source
 
- (NSInteger)numberOfSectionsInTableView:( UITableView *)tableView
{
     // Return the number of sections.
     return 5 0 ;
}
 
- (NSInteger)tableView:( UITableView *)tableView numberOfRowsInSection :(NSInteger)section
{
     return 2 ;
}
 
- ( UITableViewCell *)tableView:( UITableView *)tableView cellForRowAtIndexPath :( NSIndexPath *)indexPath
{   
     static NSString *contentIndentifer = @"Container" ;
     if (indexPath .row == 0 ) {
         UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier :contentIndentifer];
         if (cell == nil ) {
             cell = [[ UITableViewCell alloc ] initWithStyle : UITableViewCellStyleDefault reuseIdentifier :contentIndentifer];
         }
         NSString *statisticsContent = [[ NSString alloc ] initWithString : @"rlf:岁月流芳,花开几度,走在岁月里,醉在流香里,总在时光里辗转徘徊。花开几许,落花几度,岁月寒香,飘进谁的诗行,一抹幽香,掺入几许愁伤,流年似花,春来秋往,睁开迷离的双眼,回首张望,随风的尘烟荡漾着迷忙,昨日的光阴已逝去,留下无尽的回忆让人留恋与追忆" ];
         cell .textLabel .font = [ UIFont systemFontOfSize : 1 2 .0f ];
         cell .textLabel .text = statisticsContent;
         cell .textLabel .textColor = [ UIColor brownColor ]
         ;
         cell .textLabel .opaque = NO ; // 选中Opaque表示视图后面的任何内容都不应该绘制       
         cell .textLabel .numberOfLines = 8 ;       
         cell .selectionStyle = UITableViewCellSelectionStyleNone ;
         return cell;
     }
     static NSString * CellIdentifier = @"Cell" ;
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier :CellIdentifier];
     if (cell == nil ) {
         cell = [[ UITableViewCell alloc ] initWithStyle : UITableViewCellStyleDefault reuseIdentifier :CellIdentifier];
     }
     cell .imageView .image = [ UIImage imageNamed : @"ic_milestone_heart.png" ];
     cell .textLabel .text = [ NSString stringWithFormat : @"%d" ,count];
     count++;
     return cell;
}
 
//Section的标题栏高度
-(CGFloat)tableView:( UITableView *)tableView heightForHeaderInSection :(NSInteger)section
{
     if (section == 0 )
         return 4 6 ;
     else
         return 3 0 .0f ;
}
 
 
-( UIView *)tableView:( UITableView *)tableView viewForHeaderInSection :(NSInteger)section
{
     CGRect headerFrame = CGRectMake( 0 , 0 , 3 0 0 , 3 0 );
     CGFloat y = 2 ;
     if (section == 0 ) {
         headerFrame = CGRectMake( 0 , 0 , 3 0 0 , 1 0 0 );
         y = 1 8 ;
     }
     UIView *headerView = [[ UIView alloc ] initWithFrame :headerFrame];
     UILabel *dateLabel=[[ UILabel alloc ] initWithFrame :CGRectMake( 2 0 , y, 2 4 0 , 2 4 )]; //日期标签
     dateLabel .font =[ UIFont boldSystemFontOfSize : 1 6 .0f ];
     dateLabel .textColor = [ UIColor darkGrayColor ];
     dateLabel .backgroundColor =[ UIColor clearColor ];
     UILabel *ageLabel=[[ UILabel alloc ] initWithFrame :CGRectMake( 2 1 6 , y, 8 8 , 2 4 )]; //年龄标签
     ageLabel .font =[ UIFont systemFontOfSize : 1 4 .0 ];
     ageLabel .textAlignment = UITextAlignmentRight ;
     ageLabel .textColor = [ UIColor darkGrayColor ];
     ageLabel .backgroundColor =[ UIColor clearColor ];
     
     NSDateFormatter *dateFormatter = [[ NSDateFormatter alloc ] init ];
     dateFormatter .dateFormat = @"MM dd,yyyy" ;
     dateLabel .text = [ NSString stringWithFormat : @"%@" ,[dateFormatter stringFromDate :[ NSDate date ]]];
     ageLabel .text = @"1岁 2天" ;
     
     [headerView addSubview :dateLabel];
     [headerView addSubview :ageLabel];
     return headerView;
}
 
#pragma mark - Table view delegate
 
- ( void )tableView:( UITableView *)tableView didSelectRowAtIndexPath :( NSIndexPath *)indexPath
{
     if (indexPath .row == 0 ) {  
         UITableViewCell *targetCell = [tableView cellForRowAtIndexPath :indexPath];
         if (targetCell .frame .size .height == originalHeight+ 1 ){         
             [dicClicked setObject :isOpen forKey :indexPath];
         }
         else {    
             [dicClicked removeObjectForKey :indexPath];
         }      
       [ self .tableView reloadRowsAtIndexPaths :[ NSArray arrayWithObject :indexPath] withRowAnimation :UITableViewRowAnimationFade];
     }
     NSLog( @"indexPath=%@" ,indexPath);
     NSLog( @"dicClicked=%@" ,dicClicked);
}
 
-(CGFloat)tableView:( UITableView *)tableView heightForRowAtIndexPath :( NSIndexPath *)indexPath
{
     if (indexPath .row == 0 ) {
         if ([[dicClicked objectForKey :indexPath] isEqualToString : isOpen ])
             return [[dicClicked objectForKey :indexPath] floatValue ];
         else
             return originalHeight;    
     }
     else {
         return 4 5 .0f ;
     }
}
 
@end

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值