原理
折叠单元格是在原来单元格上的扩展,实际上可有UItableview和UIlabel以及一个储存数据的可变数组组成,以及控制折叠的按钮;通过按钮可以控制Uitableview的布局以及单元格的数量来实现折叠;
实现图片
代码实现和注意事项
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController <UITableViewDelegate,UITableViewDataSource>
@property (nonatomic) UIButton* btn ;
@property (nonatomic) UITableView* tableview ;
@property (nonatomic) UILabel* label ;
@property (nonatomic) NSMutableArray* arraydata ;
@end
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
UITableView* tableview01 = [[UITableView alloc] initWithFrame:CGRectMake(30, 100, 100, 50) style:UITableViewStyleGrouped] ;
[self.view addSubview:tableview01] ;
self.tableview = tableview01 ;
//
self.btn = [UIButton buttonWithType:UIButtonTypeCustom] ;
self.btn.frame = CGRectMake(130, 110, 30, 30) ;
[self.btn setImage:[UIImage imageNamed:@"xiajiantou.png"] forState:UIControlStateNormal] ;
[self.btn addTarget:self action:@selector(presstap) forControlEvents:UIControlEventTouchUpInside] ;
[self.view addSubview:self.btn] ;
[self.btn setSelected:NO] ;
//
self.tableview.delegate = self ;
self.tableview.dataSource = self ;
//
self.arraydata = [NSMutableArray arrayWithObjects:[NSString stringWithFormat:@"1"],[NSString stringWithFormat:@"2"],[NSString stringWithFormat:@"3"], [NSString stringWithFormat:@"4"] , [NSString stringWithFormat:@"5"], [NSString stringWithFormat:@"6"], [NSString stringWithFormat:@"7"], [NSString stringWithFormat:@"8"], nil] ;
self.label = [[UILabel alloc] initWithFrame:CGRectMake(30, 100, 100, 50)] ;
[self.view addSubview:self.label] ;
self.label.text = self.arraydata[0] ;
self.label.textAlignment = NSTextAlignmentCenter ;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (self.btn.selected == NO) {
return 0;
} else {
return 8;
}
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell* cell = [self.tableview dequeueReusableCellWithIdentifier:@"1"] ;
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"1"] ;
}
for (int i = 0; i < 8;i++) {
if (indexPath.row == i) {
cell.textLabel.text = self.arraydata[i] ;
}
}
return cell;
}
- (void)presstap {
if (self.btn.selected == NO) {
self.btn.selected = YES ;
self.tableview.frame = CGRectMake(30, 100, 100, 400) ;
} else {
self.btn.selected = NO ;
self.tableview.frame = CGRectMake(30, 100, 100, 50) ;
}
[self.tableview reloadData] ;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSString* str = self.arraydata[indexPath.row] ;
self.arraydata[indexPath.row] = self.arraydata[0];
self.arraydata[0] = str ;
self.label.text = self.arraydata[0] ;
[self.tableview reloadData] ;
[self presstap] ;
}
@end
注意单元格显示的地方是一个label;
以及单元格更新的时间,要同时更新cell和label ;
以及存储数据的数组内元素的交换;