Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/112.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在UITableviewcell中隐藏或显示uibutton_Ios_Uitableview_Uibutton - Fatal编程技术网

Ios 如何在UITableviewcell中隐藏或显示uibutton

Ios 如何在UITableviewcell中隐藏或显示uibutton,ios,uitableview,uibutton,Ios,Uitableview,Uibutton,我在uitableviewcell中创建了一个按钮 @interface MyMusicLibraryCell : UITableViewCell { IBOutlet UIButton * rangeBtn ; } 在.m文件中 @synthesize rangeBtn; - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { self = [s

我在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并在CellForRowatineXpath中添加一个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];
}

在按钮上添加一个
标签
,您可以按如下方式访问该按钮:

// 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=[带标签的单元格视图:100]


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

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

然后在cellForRowAtIndexPath中,使用以下命令获取对按钮的引用: btn=[带标签的单元格视图:100]


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

我正在我的项目中尝试您的代码,但它不起作用!***由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[UIButton addTarget:操作:forControlEvent:]:发送到实例0x86a5030的选择器无法识别”是否确实引用了正确的单元格?另外,确保单元格的非组件具有
标记
。好的,我修复了它。将addTarget:self更改为addTarget:null。非常感谢。我正在我的项目中尝试你的代码,但它不起作用!***由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[UIButton addTarget:操作:forControlEvent:]:发送到实例0x86a5030的选择器无法识别”是否确实引用了正确的单元格?另外,确保单元格的非组件具有
标记
。好的,我修复了它。将addTarget:self更改为addTarget:null。非常感谢。