10.13TableView3 展開和刪除 二級聯(lián)動

點(diǎn)擊cell后展開和刪除 聯(lián)動

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

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

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //因?yàn)橛袃蓚€索引,所以我們建一個數(shù)組
    _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;
    }
    //判斷關(guān)閉(刪除)
    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

如果你的產(chǎn)品經(jīng)理已經(jīng)規(guī)定了label需要的顏色字體,我們在擴(kuò)###展的時候就不要把它暴露出去,要固化。我們寫一個擴(kuò)展。

新建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出發(fā)往父視圖遞歸查找,直到找到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中實(shí)現(xiàn)

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

編譯運(yùn)行,結(jié)果如下:

Paste_Image.png

沒有暴露接口,也不需要填什么數(shù)據(jù),直接使用。

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

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

  • 因?yàn)橐Y(jié)局swift3.0中引用snapKit的問題,看到一篇介紹Xcode8,swift3變化的文章,覺得很詳細(xì)...
    uniapp閱讀 4,859評論 0 12
  • 我們在上一篇《通過代碼自定義不等高cell》中學(xué)習(xí)了tableView的相關(guān)知識,本文將在上文的基礎(chǔ)上,利用sto...
    啊世ka閱讀 1,640評論 2 7
  • 1.badgeVaule氣泡提示 2.git終端命令方法> pwd查看全部 >cd>ls >之后桌面找到文件夾內(nèi)容...
    i得深刻方得S閱讀 4,975評論 1 9
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,141評論 4 61
  • KK三部曲.反思,從失控、必然、再到這本科技想要什么,闡述了kk對科技對未來、對人類的認(rèn)識。開篇提出了科技的本質(zhì)是...
    kalala閱讀 233評論 1 1

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