ios 如何在 UITableViewCell 中为 UIButton 设置 Action

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15652133/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 22:53:37  来源:igfitidea点击:

How to set Action for UIButton in UITableViewCell

iosuitableview

提问by Romowski

I have XIBfile TimerCell.xibwith UITableViewCell. In other class in cellForRowAtIndexPathI initialize this UITableViewCell:

我有XIB文件TimerCell.xibUITableViewCell. 在cellForRowAtIndexPath 的其他类中,我初始化了这个UITableViewCell

    static NSString *CellIdentifier = @"cellTimer";
    TimerCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        //cell = [[TimerCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"TimerCell"owner:self options:nil];
        cell = [nib objectAtIndex:0];

In my TimerCellI have two UILabeland one UIButton. For this button I would like to set actionto some method.

在我的TimerCell 中,我有两个UILabel和一个UIButton。对于此按钮,我想将操作设置为某种方法。

How can I do that? And how to show in the first UILabelthe data from my background countdown timer in real time?

我怎样才能做到这一点?以及如何首先UILabel实时显示来自我的后台倒数计时器的数据?

回答by Dany

This piece of code will help you

这段代码会帮助你

static NSString *CellIdentifier = @"cellTimer";
TimerCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    //cell = [[TimerCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"TimerCell"owner:self options:nil];
    cell = [nib objectAtIndex:0];
    UIButton *button=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    button.tag=indexPath.row;
   [button addTarget:self 
       action:@selector(aMethod:) forControlEvents:UIControlEventTouchDown];
   [button setTitle:@"cellButton" forState:UIControlStateNormal];
    button.frame = CGRectMake(80.0, 0.0, 160.0, 40.0);
    [cell.contentView addSubview:button];
   }

  return cell;
}


-(void)aMethod:(UIButton*)sender
{
 NSLog(@"I Clicked a button %d",sender.tag);
}

Hope this helps!!!

希望这可以帮助!!!

回答by rob mayoff

Since you have a subclass of UITableViewCellnamed TimerCell, you can add outlet properties to TimerCell. For example:

由于您有一个UITableViewCellnamed的子类TimerCell,您可以将插座属性添加到TimerCell. 例如:

@interface TimerCell : UITableViewCell

@property (nonatomic, strong) UIButton *button;
@property (nonatomic, strong) UILabel *label;

@end

In TimerCell.xib, connect the outlets to the button and the label.

在 中TimerCell.xib,将插座连接到按钮和标签。

Then, in tableView:cellForRowAtIndexPath:, you can easily access the button to set its target and action:

然后,在 中tableView:cellForRowAtIndexPath:,您可以轻松访问按钮以设置其目标和操作:

static NSString *CellIdentifier = @"cellTimer";
TimerCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"TimerCell"owner:self options:nil];
    cell = [nib objectAtIndex:0];
}

[cell.button addTarget:self action:@selector(cellButtonWasTapped:)
    forControlEvents:UIControlEventTouchUpInside];;

You can access the cell's label using its labelproperty to update it when the timer fires.

您可以使用单元格的label属性访问单元格的标签,以便在计时器触发时更新它。

Another way to get at the button and the label is to use view tags, as I described in this answer.

获取按钮和标签的另一种方法是使用视图标签,如我在此答案中所述

In cellButtonWasTapped:(or whatever action you send from the button), you'll probably want to look up the index path of the cell containing the button. I explain how to do that in this answer.

cellButtonWasTapped:(或您从按钮发送的任何操作)中,您可能想要查找包含按钮的单元格的索引路径。我在这个答案中解释了如何做到这一点

回答by Peter Zhu

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"cellTimer";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier];
    NSInteger index = indexPath.row;
    UILabel *titleLabel = (UILabel *)[cell viewWithTag:101];
    UIButton *button = (UIButton *)[cell viewWithTag:100];
    [button addTarget:self action:@selector(MethodName:) forControlEvents:UIControlEventTouchUpInside];
}

And set UIButton and UILabel's tag in your XIB file TimerCell.xib

并在您的 XIB 文件 TimerCell.xib 中设置 UIButton 和 UILabel 的标签

回答by lreddy

Please do better research before posting on SO. Here is the code to add Button to cell

在发布到 SO 之前,请做更好的研究。这是将按钮添加到单元格的代码

UIButton *Button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[Button addTarget:self action:@selector(MethodName:) forControlEvents:UIControlEventTouchUpInside];
[Button setTitle:@"ButtonTitle" forState:UIControlStateNormal];
Button.frame = CGRectMake(100.0, 10.0, 40.0, 40.0);
[cell.contentView addSubview:Button];