先給大家看效果圖~

cell的展開.gif
這篇分享源自會飛的烏龜愛看海博主的 http://m.itdecent.cn/p/5439a989097d ,我自己稍加修改,然后在這兒呈現(xiàn)給各位看官。
本篇文章,cell的展開實際上就是改變cell的高度,然后刷新改變高度的cell所在行就行了,具體其他的效果筆者在此不做贅述,具體原因是不會,各位可根據(jù)自己的情況實現(xiàn)。
下面是代碼部分。
首先在storyBoard里面畫一個tableView,并在ViewController的延展里設(shè)為屬性。
//
// ViewController.m
// CellExpandAndShrink
//
// Created by Dom on 16/6/11.
// Copyright ? 2016年 YG. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
///上一次選中的行
@property (nonatomic, assign) NSInteger selectedRow;
///記錄被打開的行號的數(shù)組
@property (nonatomic, strong) NSMutableArray *open;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self initTableView];
}
- (NSMutableArray *)open
{
if (_open == nil) {
_open = [NSMutableArray array];
}
return _open;
}
- (void)initTableView
{
self.tableView.dataSource = self;
self.tableView.delegate = self;
}
#pragma mark - UITableView Delegate Method
// 返回cell個數(shù)
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 10;
}
// 返回每行的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([self.open containsObject:[NSString stringWithFormat:@"%ld",indexPath.row]]) {
return 100;
}
else {
return 40;
}
}
// 請求數(shù)據(jù)源代理為tableView插入需要的cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *reusid = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reusid];
if (!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reusid];
}
if ([self.open containsObject:[NSString stringWithFormat:@"%ld",indexPath.row]]) {
// 打開之后
cell.textLabel.text = @"快點關(guān)上~~~討厭~~~";
}
else {
// 正常狀態(tài)下
cell.textLabel.text = @"點擊可以打開我噢";
}
return cell;
}
// 監(jiān)聽點擊的cell
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"上一次選中的行號--%ld 本次點擊的行號--%ld",(long)self.selectedRow,(long)indexPath.row);
if ([self.open containsObject:[NSString stringWithFormat:@"%ld",indexPath.row]]) {
// 包含,說明之前已經(jīng)點擊過,這次點擊跟上一次是一樣的,從打開數(shù)組中移除
[self.open removeObject:[NSString stringWithFormat:@"%ld",indexPath.row]];
}
else {
// 不包含,說明這行是第一次點擊,直接加進去就可以了
[self.open addObject:[NSString stringWithFormat:@"%ld",indexPath.row]];
}
// 記錄點擊的行號
self.selectedRow = indexPath.row;
NSLog(@"%@",self.open);
// 刷新點擊的行
[self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
寫到這兒就沒有然后了,動態(tài)圖的效果就可以展示出來了。
這個工程比較簡單,GitHub就不傳了。最后感謝各位看官。