美文网首页
10.13TableView3 展开和删除 二级联动

10.13TableView3 展开和删除 二级联动

作者: jayck | 来源:发表于2016-10-23 21:04 被阅读41次

    点击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

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

    相关文章

      网友评论

          本文标题:10.13TableView3 展开和删除 二级联动

          本文链接:https://www.haomeiwen.com/subject/yjpkuttx.html