如何隐藏或显示uibutton在uitaiteViewCell中



我在uitaiteViewCell中创建了一个按钮

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

in .m file

@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。我应该怎么做?

tag添加到该按钮,您可以访问按下以下的按钮:

// 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];
}

将rangebtn.tag设置为某个值(例如100)。

然后在CellForrowatIndExpath中获取对按钮的引用: btn = [Cell ViewWithTag:100]

现在您可以设置该按钮的目标。

最新更新