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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在 QML 实现点击 TableView 弹出一个界面,可以通过使用模态对话框(Modal Dialog)或者其他自定义的弹出界面实现。下面是一个使用模态对话框的示例: 首先,创建一个自定义的弹出界面,比如一个叫做 PopupDialog 的组件: ```qml Rectangle { id: popupDialog width: 200 height: 150 color: "white" border.color: "black" radius: 5 // 弹出界面的内容 Text { text: "这是一个弹出界面" anchors.centerIn: parent } } ``` 然后,在 TableView 的 onClicked 信号处理函数,控制 PopupDialog 的显示和隐藏: ```qml TableView { id: tableView // 表格内容设置... // 点击 TableView 的项时触发的信号处理函数 onItemClicked: { // 创建并显示模态对话框 var popup = Qt.createQmlObject('import QtQuick.Controls 2.15; PopupDialog {}', tableView); popup.parent = tableView; popup.x = (tableView.width - popup.width) / 2; popup.y = (tableView.height - popup.height) / 2; popup.open(); // 点击对话框外部时关闭对话框 popup.clickedOutside.connect(function() { popup.close(); popup.destroy(); }); } } ``` 在上述示例,当点击 TableView 的项时,通过创建一个模态对话框 PopupDialog 来显示弹出界面。通过设置对话框的位置和大小,以及处理点击对话框外部时关闭对话框的逻辑,实现点击 TableView 弹出一个界面的效果。 你可以根据自己的需要,调整弹出界面的样式和布局,以及对话框的显示和关闭逻辑。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值