10.13TableView3 展开和删除 二级联动

点击cell后展开和删除 联动

#import "ViewController.h"
#import "CustomTableViewCell.h"

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource,CustomTableViewCellDelegate>
{
    UITableView *_tableView;
    NSMutableArray *_datas;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //因为有两个索引,所以我们建一个数组
    _datas = @[@0,@0].mutableCopy;
    
    _tableView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    [_tableView registerClass:[CustomTableViewCell class] forCellReuseIdentifier:@"cell"];
   
//    NSLog(@"-->%@",[_tableView subviews]);
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return _datas.count;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.delegate = self;
    if([_datas[indexPath.row] isEqual:@0]||[_datas[indexPath.row] isEqual:@1]){
    
        //索引等于0和1的时候
        cell.textLabel.text = @"项目";
        cell.textLabel.textColor = [UIColor blackColor];
        cell.backgroundColor = [UIColor grayColor];
    }else{
        //索引不等0和1的时候
        cell.backgroundColor = [UIColor blackColor];
        cell.textLabel.textColor = [UIColor whiteColor];
        cell.textLabel.text = @"开工······";
    }
    return cell;
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    //判断展开
    if([_datas[indexPath.row]isEqual:@0]){//exp
    
        NSMutableArray *indexPaths = [NSMutableArray array];
        for (NSInteger i = 1; i<4; i++) {
            
            [_datas insertObject:@2 atIndex:indexPath.row+1];
            NSIndexPath *customIndexPath = [NSIndexPath indexPathForRow:(indexPath.row+i) inSection:0];
            [indexPaths addObject:customIndexPath];
        }
        //在索引的地方插入,并展开动画
        [tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationTop];
      
#if 0
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.35 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            dispatch_async(dispatch_get_main_queue(), ^{
                
               [tableView reloadData];
            });
        });
#endif
        
        _datas[indexPath.row] = @1;
        
        return;
    }
    //判断关闭(删除)
    if([_datas[indexPath.row]isEqual:@1]){//close
    
        NSMutableArray *indexPaths = [NSMutableArray array];
        for (NSInteger i = 1; i<4; i++) {
            
            [_datas removeObjectAtIndex:indexPath.row+1];
            NSIndexPath *customIndexPath = [NSIndexPath indexPathForRow:(indexPath.row+i) inSection:0];
            [indexPaths addObject:customIndexPath];
        }
        
        [tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationBottom];
        _datas[indexPath.row] = @0;
        
        return;
    }
   
    if(![_datas[indexPath.row]isEqual:@0] && ![_datas[indexPath.row]isEqual:@1]){
        
        NSLog(@"*");
    }
   
}


@end

效果如下:

Paste_Image.png

如果你的产品经理已经规定了label需要的颜色字体,我们在扩###展的时候就不要把它暴露出去,要固化。我们写一个扩展。

新建CustomTableViewCell

CustomTableViewCell.h

#import <UIKit/UIKit.h>

@protocol CustomTableViewCellDelegate <NSObject>

@optional
- (void)tableViewCell:(UITableViewCell*)cell Clicked:(NSIndexPath*)indexPath;
@end

@interface CustomTableViewCell : UITableViewCell

//@property (nonatomic,strong)UILabel *contentLabel;   接口暴露,没有固化
@property (nonatomic,copy)NSString *contentString;
@property (nonatomic,weak)id <CustomTableViewCellDelegate> delegate;
@end

CustomTableViewCell.m

#import "CustomTableViewCell.h"


@interface CustomTableViewCell ()
{
    UILabel *_contentLabel;
    UIButton *_button;
}
@end

@implementation CustomTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{

    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        _contentLabel = [UILabel new];
        [self.contentView addSubview:_contentLabel];
        _contentLabel.frame  = CGRectMake(5, 5, 100, 30);
        _contentLabel.backgroundColor = [UIColor redColor];
        
        _button = [UIButton buttonWithType:UIButtonTypeCustom];
        [_button setTitle:@"button" forState:UIControlStateNormal];
        _button.backgroundColor = [UIColor blueColor];
        [_button setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
        [_button addTarget:self action:@selector(buttonCliced:) forControlEvents:UIControlEventTouchUpInside];
        _button.frame = CGRectMake(200, 5, 100, 20);
        [self.contentView addSubview:_button];
    }
    
    return self;
}

- (void)buttonCliced:(UIButton*)sender{

    CGPoint point = [sender convertPoint:CGPointZero toView:[self findTableView]];
    
    //找到索引位置就可以传值出去,可是没有TableView怎么传?
    //我们用下面的那个findTableView自己写的方法递归查找到UITableView
    NSIndexPath *indexPath = [[self findTableView] indexPathForRowAtPoint:point];
    if([self.delegate respondsToSelector:@selector(tableViewCell:Clicked:)]){
        
        [self.delegate tableViewCell:self Clicked:indexPath];
    }
//    NSLog(@"--->%@",[self findTableView]);
}

- (UITableView*)findTableView{

    UIResponder *responder = self;
    //从cell出发往父视图递归查找,直到找到UITableView
    while (![responder isKindOfClass:[UITableView class]]) {
        
        responder = responder.nextResponder;
    }
    
    return (UITableView*)responder;
}


- (void)setContentString:(NSString *)contentString{

    _contentLabel.text = contentString;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end

在ViewController.m中实现

- (void)tableViewCell:(UITableViewCell *)cell Clicked:(NSIndexPath *)indexPath{
    
    NSLog(@"%ld",indexPath.row);
}

编译运行,结果如下:

Paste_Image.png

没有暴露接口,也不需要填什么数据,直接使用。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 200,302评论 5 470
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 84,232评论 2 377
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 147,337评论 0 332
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,977评论 1 272
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,920评论 5 360
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,194评论 1 277
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,638评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,319评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,455评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,379评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,426评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,106评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,696评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,786评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,996评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,467评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,043评论 2 341

推荐阅读更多精彩内容

  • 因为要结局swift3.0中引用snapKit的问题,看到一篇介绍Xcode8,swift3变化的文章,觉得很详细...
    uniapp阅读 4,377评论 0 12
  • 我们在上一篇《通过代码自定义不等高cell》中学习了tableView的相关知识,本文将在上文的基础上,利用sto...
    啊世ka阅读 1,489评论 2 7
  • 1.badgeVaule气泡提示 2.git终端命令方法> pwd查看全部 >cd>ls >之后桌面找到文件夹内容...
    i得深刻方得S阅读 4,624评论 1 9
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 11,982评论 4 60
  • KK三部曲.反思,从失控、必然、再到这本科技想要什么,阐述了kk对科技对未来、对人类的认识。开篇提出了科技的本质是...
    kalala阅读 152评论 1 1