获取cell中按钮的交互事件

有时候在自定义Cell的时候 会有按钮,而按钮需要交互,有时候是一个按钮,两个按钮,根据实际需求,我这边用自定Cell中 有两个UIButton的控件,获取他们的交互事件。方法很多,我这边使用代理实现
这里写图片描述
思路:设置UIButton的tag值,这边使用indexPath的section或者indexPath的row来作为UIButton的tag值, 然后在cell中设置代理,在Controller中实现代理就OK。

1.在下面方法中设置UIButton的tag

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *addProjectIdentifier = @"addProjectCell";
            [tableView registerClass:[MessageAddProjectsTableViewCell class] forCellReuseIdentifier:addProjectIdentifier];
            MessageAddProjectsTableViewCell *addProjectCell = [tableView dequeueReusableCellWithIdentifier:addProjectIdentifier forIndexPath:indexPath];
            [addProjectCell loadCellMessageMessageSystemModel:model];
            addProjectCell.delegate = self; // 设置代理
            addProjectCell.backgroundColor = [UIColor clearColor];
            addProjectCell.rejectButton.tag = indexPath.section;
            addProjectCell.agreeButton.tag = indexPath.section; // 设置tag值
            addProjectCell.selectionStyle = UITableViewCellSelectionStyleNone;
            return addProjectCell;
}

2.在cell.h中设置代理

@protocol CellButtonDeleage

/**
 *  拒绝添加到项目组
 *
 *  @param button UIBuuton
 */
- (void)rejectAddProjects:(UIButton *)button;

/**
 *  同意添加到项目组
 *
 *  @param button UIButton
 */
- (void)agressAddProjects:(UIButton *)button;

@end

@interface MessageAddProjectsTableViewCell : UITableViewCell

@property (assign, nonatomic) id delegate; // 代理
@property (strong, nonatomic) UIButton *rejectButton; // 拒绝按钮
@property (strong, nonatomic) UIButton *agreeButton; // 同意按钮
@end

3.在cell.m中的UIButton交互事件中设置代理传值

/**
 *  同意添加到项目组
 *
 *  @param button UIButton
 */
- (void)agreeAction:(UIButton *)button
{
    GFBLog(@"同意添加到项目组");
    if ([_delegate respondsToSelector:@selector(agressAddProjects:)]) {
        button.tag = self.agreeButton.tag;
        [_delegate agressAddProjects:button];
    }
}


/**
 *  拒绝添加到项目组
 *
 *  @param button UIButton
 */
- (void)rejectAction:(UIButton *)button
{
    GFBLog(@"拒绝添加到项目组");
    if ([_delegate respondsToSelector:@selector(rejectAddProjects:)]) {
        button.tag = self.rejectButton.tag;
        [_delegate rejectAddProjects:button];
    }
}

4.在controller中实现代理方法

#pragma mark - CellButtonDeleage
/**
 *  拒绝添加到项目组
 *
 *  @param button UIButton
 */
-  (void)rejectAddProjects:(UIButton *)button
{
    GFBLog(@"拒绝添加到项目组 delegate");
    YJMessageSystemModel *model = [self.messafeSystemArrayModel objectAtIndex:button.tag];
    GFBLog(@"消息UUID --- %@", model.uuid);
    [self sendRejectOrAgressRequest:model.uuid]; // 网络请求

}

/**
 *  同意添加到项目组
 *
 *  @param button UIButton
 */
- (void)agressAddProjects:(UIButton *)button
{
    GFBLog(@"同意添加到项目组 delegate");
    YJMessageSystemModel *model = [self.messafeSystemArrayModel objectAtIndex:button.tag];
    GFBLog(@"消息UUID == %@", model.uuid);
    self.agressFlag = YES;
    [self sendRejectOrAgressRequest:model.uuid];
}

打印结果
这里写图片描述

这样就完成了,可以在代理方法中实现UIButton的交互操作,当然也可以才cell的交互事件中实现实现,但是那样耦合性高。我今天刚刚使用这个这个代理来实现,在看博客时候的,看到,就写了下!记录自己的成长过程····如有帮助请笑纳,如有不足,还望提点!谢谢。慢慢会发觉 有思路了 一切就顺了!

今天还遇到一个很愚蠢的事,Cell上的按钮不出来,搞了我一小会,然后看层次结构出来的,我知道被什么遮住,最后是因为拷贝的··多覆盖了一层,由此可见,尽量不要复制代码,有时候好忧伤的!找出来的错误比自己重写一遍还难 谨记…

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
在PyQt5,可以通过在表格添加按钮来实现特定的交互功能。下面是一个简单的示例,可以在表格添加按钮并为其添加点击事件: ```python from PyQt5.QtWidgets import QApplication, QMainWindow, QTableWidget, QTableWidgetItem, QPushButton from PyQt5.QtCore import Qt class MainWindow(QMainWindow): def __init__(self): super().__init__() self.table = QTableWidget() self.table.setColumnCount(3) self.table.setRowCount(2) for i in range(self.table.rowCount()): for j in range(self.table.columnCount()-1): self.table.setItem(i, j, QTableWidgetItem("Cell {0}-{1}".format(i, j))) for row in range(self.table.rowCount()): button = QPushButton("Button {0}".format(row)) button.setProperty("row", row) button.clicked.connect(self.handleButtonClicked) self.table.setCellWidget(row, self.table.columnCount()-1, button) self.setCentralWidget(self.table) def handleButtonClicked(self): button = self.sender() row = button.property("row") print("Button {0} clicked in row {1}".format(button.text(), row)) if __name__ == "__main__": app = QApplication([]) window = MainWindow() window.show() app.exec_() ``` 在这个示例,我们创建了一个包含3列和2行的表格,并在前两列添加了文本单元格。我们然后遍历表格的每一行,并为每一行添加一个按钮。我们使用`setProperty`方法为每个按钮存储一个`row`属性,以便在按钮被点击时可以轻松地获取行号。最后,我们将按钮添加到表格的第三列,并连接每个按钮的`clicked`信号到我们的`handleButtonClicked`方法上。 `handleButtonClicked`方法获取被点击的按钮并从获取行号。通过这种方式,您可以轻松地在表格实现各种交互功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

建古

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值