簡體   English   中英

如何在UITableviewcell中隱藏或顯示uibutton

[英]How to hide or show uibutton in UITableviewcell

我在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。我該怎么做?

在按鈕上添加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]

現在,您可以為該按鈕設置目標。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM