点击tableView的cell弹出tableView

 在项目中总会用到点击大的TableView弹出一个小的TableView,将点击的小的TableViewcell内容传回大的TableViewCell上 最近项目需求 就写了一个demo 供大家参考

1、首先 我们建立一个View来承载这个小的tableview

@protocol HYJNegotiateViewDelegate<NSObject>

/**

 *  建立代理方法 这里是将点击的cell内容记录下来

 */

-(void)HYJxxiangqingSizevviewGouwuChe :(NSString *)sender;


@end

@interface HYJNegotiateView : UIView<UITableViewDataSource,UITableViewDelegate>

/**

 *  显示选项

 */

@property (weak, nonatomic) IBOutlet UITableView *tableViewMy;

@property (weaknonatomicIBOutlet UITapGestureRecognizer *tapGesture;


/**

 *  背景

 */

@property (weak, nonatomic) IBOutlet UIView *viewBackGround;

@property (strong, nonatomicNSArray *arrayData;

@property(nonatomic,weak)id<HYJNegotiateViewDelegate>delegate;

/**

 *  这里是实例化方法 创建cell用

 */

+(HYJNegotiateView *)instanceSizeTextView;

在.m文件中进行编辑


+(HYJNegotiateView *)instanceSizeTextView

{

    NSArray* nibView =  [[NSBundle mainBundle] loadNibNamed:@"HYJNegotiateView" owner:nil options:nil];

    return [nibView objectAtIndex:0];

}

-(void)awakeFromNib{

    _tableViewMy.delegate = self;

    _tableViewMy.dataSource = self;

    _arrayData = @[@"完成",@"未完成待料",@"未完成待修",@"未完成完工"];

    _tableViewMy.tableFooterView = [[UIView alloc]init];

}

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

{

    return 4;

}

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

{

    static NSString *ID =@"UITableViewCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];

    if (cell == nil) {

        cell = [[UITableViewCell alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:ID];

    }

    cell.textLabel.text = _arrayData[indexPath.row];

    return cell;

    

}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 44;

}

//在这里安全起见 对代理的参数方法进行判断 将点击的cell 获取到的内容(cell.textLabel.text)传回tableview

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

    UITableViewCell*cell = [tableView cellForRowAtIndexPath:indexPath];

    if (self.delegate &&[self.delegate respondsToSelector:@selector(HYJxxiangqingSizevviewGouwuChe:)]) {

        [self.delegate HYJxxiangqingSizevviewGouwuChe:cell.textLabel.text];

    }


}

好  这个view我们就编写完了  我们接下来对控制器进行操作一下,这里我们用到了runtime

#import <objc/runtime.h>

static char cellKey;

头文件包含这些内容,在点击cell的方法中实现这个方法

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

    if (indexPath.row == 3) {

        HYJNegotiateView *size =[HYJNegotiateView instanceSizeTextView];

        size.delegate = self;

        size.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width,[UIScreen mainScreen].bounds.size.height);

        [size.viewBackGround addGestureRecognizer:size.tapGesture];

        [size.tapGesture addTarget:self action:@selector(oneBtnsMethod)];

        [[UIApplication sharedApplication].keyWindow addSubview:size];

        self.negotiate = size;

        objc_setAssociatedObject(self, &cellKey, indexPath, OBJC_ASSOCIATION_RETAIN);//主要保存对indexPath的储存

    }


}

点击cell之后就消失

-(void)oneBtnsMethod{

    [self.negotiate removeFromSuperview];

    [self.negotiate.viewBackGround removeFromSuperview];

}

在控制器中 实现代理方法

- (void)HYJxxiangqingSizevviewGouwuChe:(NSString *)sender{


    NSIndexPath *indexPath = (NSIndexPath*)objc_getAssociatedObject(self, &cellKey);

    UITableViewCell *cell = [_tableViewMy cellForRowAtIndexPath:indexPath];

    cell.detailTextLabel.text = sender;

    

    [self oneBtnsMethod];

}

控制器中的tableview的数据,cell展示方式可自行定制,在这里就不多讲了,我用的是系统的cell;

猜你喜欢

转载自blog.csdn.net/chungeshihuatian/article/details/51680286
今日推荐