点击tableView的cell弹出tableView

来源:互联网 发布:直播弹幕语音助手 mac 编辑:程序博客网 时间:2024/04/28 09:38

 在项目中总会用到点击大的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) IBOutletUITableView *tableViewMy;

@property (weaknonatomicIBOutlet UITapGestureRecognizer *tapGesture;


/**

 *  背景

 */

@property (weak,nonatomic) IBOutletUIView *viewBackGround;

@property (strong,nonatomicNSArray *arrayData;

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

/**

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

 */

+(HYJNegotiateView *)instanceSizeTextView;

在.m文件中进行编辑


+(HYJNegotiateView *)instanceSizeTextView

{

    NSArray* nibView =  [[NSBundlemainBundle] loadNibNamed:@"HYJNegotiateView"owner:niloptions:nil];

    return [nibViewobjectAtIndex:0];

}

-(void)awakeFromNib{

    _tableViewMy.delegate =self;

    _tableViewMy.dataSource =self;

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

    _tableViewMy.tableFooterView = [[UIViewalloc]init];

}

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

{

    return4;

}

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

{

    staticNSString *ID =@"UITableViewCell";

    UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:ID];

    if (cell ==nil) {

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

    }

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

    return cell;

    

}

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

    return44;

}

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

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

    UITableViewCell*cell = [tableViewcellForRowAtIndexPath:indexPath];

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

        [self.delegateHYJxxiangqingSizevviewGouwuChe: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 =[HYJNegotiateViewinstanceSizeTextView];

        size.delegate =self;

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

        [size.viewBackGroundaddGestureRecognizer:size.tapGesture];

        [size.tapGestureaddTarget:selfaction:@selector(oneBtnsMethod)];

        [[UIApplicationsharedApplication].keyWindowaddSubview:size];

        self.negotiate = size;

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

    }


}

点击cell之后就消失

-(void)oneBtnsMethod{

    [self.negotiateremoveFromSuperview];

    [self.negotiate.viewBackGroundremoveFromSuperview];

}

在控制器中 实现代理方法

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


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

    UITableViewCell *cell = [_tableViewMycellForRowAtIndexPath:indexPath];

    cell.detailTextLabel.text = sender;

    

    [selfoneBtnsMethod];

}

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

0 0
原创粉丝点击