如何隐藏或显示行选择按钮?

Posted

技术标签:

【中文标题】如何隐藏或显示行选择按钮?【英文标题】:How to hide or show button on row selection? 【发布时间】:2013-09-06 09:14:14 【问题描述】:

我是 ios 开发的新手,我有一个自定义表格视图,上面放置了 3 个按钮,

    添加评论 向上箭头 向下箭头

我想在按下向下箭头按钮时增加单元格的高度,同时使添加评论按钮,向下箭头按钮可见。

我的代码,

#import "JCoutGoingConfess.h"

@interface JCoutGoingConfess (private)
- (BOOL)cellIsSelected:(NSIndexPath *)indexPath;
@end

@implementation JCoutGoingConfess
#define kCellHeight 132.0

@synthesize outConfessionNavbarBAckBtn,JCoutGoingTbl;

//---------------------------- TableView---------------------------------------
- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths
              withRowAnimation:(UITableViewRowAnimation)animation

    NSIndexPath* rowToReload = [NSIndexPath indexPathForRow:1 inSection:0];
    NSArray* rowsToReload = [NSArray arrayWithObjects:rowToReload, nil];


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

    return 5;


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

    // If our cell is selected, return double height
    if([self cellIsSelected:indexPath]) 
        return kCellHeight * 2.0;
    
    return kCellHeight;


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

    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) 
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                            reuseIdentifier:CellIdentifier];
    
    // ----------------------- cellbackground image-----------------------------
    if([self cellIsSelected:indexPath])
        cell.backgroundView =[[UIImageView alloc]initWithFrame:CGRectMake(160, 151, 320, 108)];
        cell.backgroundView = [[UIImageView alloc] initWithImage: [UIImage imageNamed:@"inCellFullBg.png"]];
    
    //         ********   imageView on cell    *******
    UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(112,0, 90, 90)];
    imgView.image = [UIImage imageNamed:@"friendsfacepic.png"];
    [cell.contentView addSubview:imgView];

    //----------------------------Cell buttons-------------------------------------------
    if (JcOutCellSize==152)
    

        UIButton *AddComment = [UIButton buttonWithType:UIButtonTypeCustom];
        [AddComment addTarget:self
                       action:@selector(TestBtns:)
             forControlEvents:UIControlEventTouchDown];

        AddComment.frame = CGRectMake(9.0, 128.0, 96.0, 26.0);
        [cell.contentView addSubview:AddComment];

        UIImage *buttonAddComment = [UIImage imageNamed:@"addcomment.png"];
        [AddComment setBackgroundImage:buttonAddComment forState:UIControlStateNormal];
        [cell.contentView addSubview:AddComment];

        UIButton *UpArrow = [UIButton buttonWithType:UIButtonTypeCustom];
        [UpArrow addTarget:self
                    action:@selector(ResizeCell:)
          forControlEvents:UIControlEventTouchDown];
        //[DownArrow setTitle:@"Arrow" forState:UIControlStateNormal];
        UpArrow.frame = CGRectMake(143.0, 136.0, 26.0, 16.0);
        [cell.contentView addSubview:UpArrow];

        UIImage *buttonUp = [UIImage imageNamed:@"upArrow.png"];
        [UpArrow setBackgroundImage:buttonUp forState:UIControlStateNormal];
        [cell.contentView addSubview:UpArrow];
    

    //----------------------------------------------------------------------------------
    if (JcOutCellSize==132)
    
        NSLog(@" down arrow %f",JcOutCellSize);
        UIButton *DownArrow = [UIButton buttonWithType:UIButtonTypeCustom];
        [DownArrow addTarget:self
                      action:@selector(IncreseCellHight:)
            forControlEvents:UIControlEventTouchDown];
        DownArrow.frame = CGRectMake(143.0, 116.0, 26.0, 16.0);
        [cell.contentView addSubview:DownArrow];

        UIImage *buttondown = [UIImage imageNamed:@"upDownArrow.png"];
        [DownArrow setBackgroundImage:buttondown forState:UIControlStateNormal];
        [cell.contentView addSubview:DownArrow];
        NSLog(@" cell size = %f",JcOutCellSize);
    




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

    [tableView deselectRowAtIndexPath:indexPath animated:TRUE];
    // Toggle 'selected' state
    BOOL isSelected = ![self cellIsSelected:indexPath];
    NSNumber *selectedIndex = [NSNumber numberWithBool:isSelected];
    [selectedIndexes setObject:selectedIndex forKey:indexPath];
    [JCoutGoingTbl beginUpdates];
    [JCoutGoingTbl endUpdates];




- (void)viewDidLoad

    selectedIndexes = [[NSMutableDictionary alloc] init];
    [super viewDidLoad];



- (void)viewDidUnload

    selectedIndexes = nil;
    [super viewDidUnload];


- (BOOL)cellIsSelected:(NSIndexPath *)indexPath 
    // Return whether the cell at the specified index path is selected or not
    NSNumber *selectedIndex = [selectedIndexes objectForKey:indexPath];
    return selectedIndex == nil ? FALSE : [selectedIndex boolValue];

这没有按预期工作,可能是什么问题?

【问题讨论】:

谁有勇气读这本书? 准确点,指出问题所在,添加大量代码支持您的问题 我给出这个是为了解释整个问题。谢谢 @PratapManishBhadoria 请重构您的代码。它很难阅读。例如删除不必要的空格。 【参考方案1】:

在按钮操作中设置任何整数变量值并将条件放入heightForRowAtIndexPath,无论您想要什么大小,然后重新加载表格

【讨论】:

有什么问题请告诉我 UIButton *DownArrow 在 .h 和您的方法 IncreseCellHight 中清除此内容:write downArrow.hidden = yes; 更改您的问题描述,以便我可以相应地回答您 向下箭头按钮没有隐藏请建议我。 我已经提供了代码,请从上往下看。

以上是关于如何隐藏或显示行选择按钮?的主要内容,如果未能解决你的问题,请参考以下文章

jQuery:如何以编程方式隐藏 TableTools 按钮

根据行内容显示或隐藏 DGrid 选择器

如何在 DataGridView 中禁用或隐藏按钮

根据取货和交付按钮显示或隐藏 WooCommerce 结帐字段

Excel隐藏多行,如何一次全部展开显示或取消隐藏

在 SKScene 中隐藏和取消隐藏按钮