如何为UITableViewCell中的UIButton设置操作。

15

我有一个 XIB 文件,名称为 TimerCell.xib,其中包含一个 UITableViewCell。在另一个类中的 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];

在我的TimerCell中,我有两个UILabel和一个UIButton。对于这个按钮,我想设置action到某个方法。

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

4个回答

26

这段代码将会帮助你。

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);
}
希望这可以帮助!

它可以正常工作!谢谢!现在如何设置标记?之前是 cell.staticButton.tag = indexPath.row; - Romowski

15

由于您有一个名为 TimerCellUITableViewCell 子类,因此可以向 TimerCell 添加 outlet 属性。例如:

@interface TimerCell : UITableViewCell

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

@end

TimerCell.xib中,将Outlets连接到Button和Label。然后,在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];;

当计时器触发时,您可以使用单元格的label属性访问其标签以进行更新。

另一种获取按钮和标签的方法是使用视图标记,就像我在此答案中所描述的那样。

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


我不明白如何在UITableViewCell中显示当前计时器值...你能再解释一下吗?更多细节:计时器在单例中工作。只有一个计时器,但是有一个包含时间值的字典中有几个项。每分钟计时器从字典中所有项中递减1. TimerCell是出现在弹出窗口中的tableView的单元格。所以我想在我的cell.label中显示字典项的时间值...谢谢 - Romowski
http://stackoverflow.com/questions/15653729/how-to-show-nstimers-time-value-in-uitableviewcells-label - Romowski
+1 给你出色的解释和文档... 还有 10 分与你一同前行,达到 119,000 ;) - logixologist

2
-(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];
}

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


-3
请在在发布问题前做更多的研究。
以下是将按钮添加到单元格中的代码。
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];

为什么它被踩了?请解释一下,否则它就没什么用了。是错的吗?它回答或试图回答这个问题吗? - YSC

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接