15

我有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];

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

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

4

4 回答 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);
}

希望这可以帮助!!!

于 2013-03-27T05:00:16.147 回答
15

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

@interface TimerCell : UITableViewCell

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

@end

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

然后,在 中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:(或您从按钮发送的任何操作)中,您可能想要查找包含该按钮的单元格的索引路径。我在这个答案中解释了如何做到这一点。

于 2013-03-27T05:10:23.593 回答
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 的标签

于 2013-03-27T08:58:32.093 回答
-3

请在 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];
于 2013-03-27T05:08:21.337 回答