如何通过点击UITableViewCell中的一个按钮来实现UITableViewCell选择的效果? [英] How to achieve the effect of UITableViewCell selection by clicking a button in UITableViewCell?

查看:26
本文介绍了如何通过点击UITableViewCell中的一个按钮来实现UITableViewCell选择的效果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在列表视图控制器中有一个 menuView.当单元格中的更多按钮被录音时,在 UITableViewCell 上添加的 menuView.

I have a menuView in a list view controller. The menuView added on the UITableViewCell when a more button in the cell being taped.

用单例很容易实现.这是代码:

It is easy to achieve with singleton. Here is code:

@implementation ProductsOperationMenu
static ProductsOperationMenu *_instance;
+ (instancetype)sharedInstance{

    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [[self alloc] initWithFrame:CGRectZero];
    });
    return _instance;
}


- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self setup];
    }
    return self;
}

ZBMyProductsCell.m

@implementation ZBMyProductsCell

- (void)awakeFromNib
{
    [super awakeFromNib];
    _operationMenu = [[ProductsOperationMenu alloc] initWithFrame: CGRectZero];
}

- (IBAction)operationButtonClick:(UIButton *)sender {
    if ([self.contentView.subviews containsObject:_operationMenu]) {
        _operationMenu.hidden = ![_operationMenu isHidden];
    } else{
        [self.contentView addSubview:_operationMenu];
        _operationMenu.hidden = NO;
    }

    [_operationMenu mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(205);
        make.height.mas_equalTo(60);
        make.bottom.mas_equalTo(self.operationButton).offset(0);
        make.right.mas_equalTo(self.operationButton.mas_left).offset(-10);
    }];
}

我认为这是单身滥用.我想改进代码.

I think it is singleton abuse. I want to improve the code.

没有singleton,代码和效果:

Without singleton, the code and effect:

@implementation ProductsOperationMenu
- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self setup];
    }
    return self;
}

我想我必须处理单元格之间的消息发送.当一个单元格的按钮被点击时,其他单元格的菜单视图必须隐藏.

I think I have to handle the message sending between cells. When one cell's button being clicked, the menu view of others must hide.

我认为它与单元格的选择非常相似.当一个单元格被选中时,前一个选中的单元格效果消失.

I think it is quite similar to cell's selection. When one cell is being selected , the previous selected one's effect dismissed.

无或有

那么如何通过点击UITableViewCell中的一个按钮来实现UITableViewCell选择的效果呢?

So how to achieve the effect of UITableViewCell selection by clicking a button in UITableViewCell?

推荐答案

创建一个全局整数,其中包含被单击的单元格的当前索引,当单击单元格中的任何按钮时,更改索引并重新加载 tableView -- 在 cellForRow 中

Make a global integer that contains the currently index of cell being clicked and when any button in cell is being clicked change the index and reload the tableView -- in cellForRow

 if(indexpath.row == index)
 {

    cell.menuView.isHidden = false
 }
 else
 {

    cell.menuView.isHidden = true
 }

如果您想在选择另一个时为 menuView 隐藏动画,那么您必须使 tableview 全局化并访问他的可见单元格并使用持续时间为 menuView 的 alpha 设置动画

If you want to animate hide of menuView when another is selected then you must make the tableview global and access his visible cells and animate alpha of menuView with duration

这篇关于如何通过点击UITableViewCell中的一个按钮来实现UITableViewCell选择的效果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆