點擊Cell控制UITableViewCell的展開及關(guān)閉

先給大家看效果圖~

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就不傳了。最后感謝各位看官。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容