如何在UITableViewCell中为UIButton设置Action [英] How to set Action for UIButton in UITableViewCell

查看:101
本文介绍了如何在UITableViewCell中为UIButton设置Action的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 XIB 文件 TimerCell.xib ,带有 UITableViewCell 。在 cellForRowAtIndexPath 中的其他类中,我初始化此 UITableViewCell

I have XIB file TimerCell.xib with UITableViewCell. In other class in cellForRowAtIndexPath I initialize this 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];

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

In my TimerCell I have two UILabel and one UIButton. For this button I would like to set action to some method.

我该怎么做?以及如何在第一个 UILabel 中实时显示来自我的后台倒数计时器的数据?

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

推荐答案

这段代码可以帮助你

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!!!

这篇关于如何在UITableViewCell中为UIButton设置Action的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆