Ios 如何在UITableViewCell中设置UIButton的操作

Ios 如何在UITableViewCell中设置UIButton的操作,ios,uitableview,Ios,Uitableview,我有XIB文件TimerCell.XIB和UITableViewCell。在单元格中的其他类中,我初始化此UITableViewCell: static NSString *CellIdentifier = @"cellTimer"; TimerCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { //cell = [[Ti

我有
XIB
文件TimerCell.XIB
UITableViewCell
。在单元格中的其他类中,我初始化此
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];
在myTimerCell中,我有两个
UILabel
和一个
UIButton
。对于此按钮,我想将操作设置为某种方法


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

这段代码将帮助您

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);
}

希望这有帮助

请在发布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];

由于您有一个子类
UITableViewCell
名为
TimerCell
,因此可以将插座属性添加到
TimerCell
。例如:

@interface TimerCell : UITableViewCell

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

@end
TimerCell.xib
中,将插座连接到按钮和标签上

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

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];;
您可以使用单元格的
label
属性访问单元格的标签,以便在计时器启动时对其进行更新

获取按钮和标签的另一种方法是使用视图标记,如中所述

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


并在您的XIB文件TimerCell.XIB中设置UIButton和UILabel的标记,它可以工作!非常感谢。现在如何设置标签?之前是cell.staticButton.tag=indexPath.row;我不明白如何在UITableViewCell中显示当前计时器值。。。你能再解释一下吗。有关更多详细信息:计时器在singleton中工作。只有一个计时器,但有一个包含多个包含时间值的项的字典。每分钟计时器从字典中的所有项中减去1。TimerCell是在弹出窗口中显示的tableView单元格。所以我想在我的单元格中显示字典项的时间值。标签。。。感谢+1的精彩解释和文档。。。你还有10个(119000;)为什么被否决?请解释一下,否则就没用了。错了吗?它是回答还是试图回答这个问题?
-(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];
}