我在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/