折叠cell
在暑假任务3Gshare中用到了折叠cell;
具体效果如下:
首先设定两个属性为:tableView和存储cell的元素数据的可变数组。
@interface ViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableview1;
@property (nonatomic, strong) NSMutableArray *array;
@end
然后对两个属性进行初始化,注意对tableView1的tag只进行设定,从而通过tag的值来判断cell的展开还是收缩状态。
self.tableview1 = [[UITableView alloc] initWithFrame:CGRectMake(100, 100, 430 - 200, 100)];
self.tableview1.delegate = self;
self.tableview1.dataSource = self;
self.tableview1.tag = 0;
[self.tableview1 registerClass:[UITableViewCell class] forCellReuseIdentifier:@"1"];
[self.view addSubview:_tableview1];
_array = [NSMutableArray arrayWithObjects:@"原创作品",@"设计教程",@"设计资料",@"设计师观点", nil];
然后要完成tableView的协议函数:
//获取每组单元格的个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (_tableview1.tag == 0) {
return 1;
} else {
return 4;
}
}
//获取单元格高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 20;
}
然后往cell中填充不同的信息:
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [self.tableview1 dequeueReusableCellWithIdentifier:@"1" forIndexPath:indexPath];
cell.textLabel.text = _array[indexPath.row];
cell.backgroundColor = [UIColor whiteColor];
return cell;
}
然后写点击cell以及数据交换的相关方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (_tableview1.tag == 0) {
_tableview1.frame = CGRectMake(100, 100, 430 - 200, 400);
_tableview1.tag = 1;
[_tableview1 reloadData];
} else {
UITableViewCell *cell0 = [self.tableview1 cellForRowAtIndexPath:indexPath];
NSString *str = [NSString stringWithFormat:@"%@", cell0.textLabel.text];
for (int i = 0; i < 4; i++) {
if ([_array[i] isEqualToString:str]) {
_array[i] = _array[0];
break;
}
}
_array[0] = str;
_tableview1.frame = CGRectMake(100, 100, 430 - 200, 100);
_tableview1.tag = 0;
[_tableview1 reloadData];
}
}
全部代码如下:
//ViewController.h文件
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableview1;
@property (nonatomic, strong) NSMutableArray *array;
@end
//ViewController.m文件
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.tableview1 = [[UITableView alloc] initWithFrame:CGRectMake(100, 100, 430 - 200, 100)];
self.tableview1.delegate = self;
self.tableview1.dataSource = self;
self.tableview1.tag = 0;
[self.tableview1 registerClass:[UITableViewCell class] forCellReuseIdentifier:@"1"];
[self.view addSubview:_tableview1];
_array = [NSMutableArray arrayWithObjects:@"原创作品",@"设计教程",@"设计资料",@"设计师观点", nil];
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [self.tableview1 dequeueReusableCellWithIdentifier:@"1" forIndexPath:indexPath];
cell.textLabel.text = _array[indexPath.row];
cell.backgroundColor = [UIColor whiteColor];
return cell;
}
//获取每组单元格的个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (_tableview1.tag == 0) {
return 1;
} else {
return 4;
}
}
//获取单元格高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 20;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (_tableview1.tag == 0) {
_tableview1.frame = CGRectMake(100, 100, 430 - 200, 400);
_tableview1.tag = 1;
[_tableview1 reloadData];
} else {
UITableViewCell *cell0 = [self.tableview1 cellForRowAtIndexPath:indexPath];
NSString *str = [NSString stringWithFormat:@"%@", cell0.textLabel.text];
for (int i = 0; i < 4; i++) {
if ([_array[i] isEqualToString:str]) {
_array[i] = _array[0];
break;
}
}
_array[0] = str;
_tableview1.frame = CGRectMake(100, 100, 430 - 200, 100);
_tableview1.tag = 0;
[_tableview1 reloadData];
}
}
@end