ios - 如何在 UITableviewcell 中隐藏或显示 uibutton

标签 ios uitableview uibutton

我在uitableviewcell中创建了一个按钮

@interface MyMusicLibraryCell : UITableViewCell
{
    IBOutlet UIButton * rangeBtn ;
}

在.m文件中

@synthesize rangeBtn;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code 
        rangeBtn = [[UIButton alloc] init];
        rangeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [rangeBtn setBackgroundImage:[UIImage imageNamed:@"fav_4.png"] forState:UIControlStateNormal];
        rangeBtn.frame = CGRectMake(260.0f, 10.0f, 20.0f, 20.0f);
        [self addSubview:rangeBtn];
    }

    return self;
}

我想使用 rangeBtn 并在 cellForRowAtIndexPath 中添加一个 addTarget。我应该怎么做?

最佳答案

给按钮添加一个标签,您可以访问按钮如下:

// initWithStyle
rangeBtn.tag = 1;


// cellForRowAtIndexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: @"cell"];
    UIButton *cellButton = (UIButton *)[cell viewWithTag: 1];

    // add target to the button
    [cellButton addTarget: self action: @selector(buttonPressed:) forControlEvent: UIControlEventTouchUpInside];
}

关于ios - 如何在 UITableviewcell 中隐藏或显示 uibutton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13150972/

相关文章:

ios - 访问导致快速崩溃的按钮操作内的单元格

swift - 存在后退按钮时无法设置 UINavigationBar 标题或将 UIButtons 放置在 UINavigationBar 中

ios - 使用 OpenGL ES 平滑改变线宽

ios - 无法在SpriteKit的SKLabelNode的FontColor中使用UIColor(red:,green:,blue :,)

ios - 如何在 NSArray 的第一个索引处添加对象

iphone - 如何更改 Controller 中按钮的图像与另一个按钮的图像?

ios - 分配不同的按钮,不同的列有不同的行为

ios - Cocos2d V3 iOS - 如何从 App Delegate 访问 runningScene

ios - 设置 UITableView 委托(delegate)和数据源时的最佳实践

uitableview - 使用 ObjC 将 indexPathForCell 无法识别的选择器发送到 iOS 8 上的实例