如何在UITableViewCell中为UIButton设置Action
我有XIT文件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
。 对于这个button,我想设置一些方法的行动 。
我怎样才能做到这一点? 以及如何在第一个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); }
希望这可以帮助!!!
由于您有一个名为TimerCell
的UITableViewCell
的子类,您可以将出口属性添加到TimerCell
。 例如:
@interface TimerCell : UITableViewCell @property (nonatomic, strong) UIButton *button; @property (nonatomic, strong) UILabel *label; @end
在TimerCell.xib
,将sockets连接到button和标签。
然后,在tableView:cellForRowAtIndexPath:
,可以轻松访问button来设置其目标和操作:
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
属性访问单元格的标签,以在定时器触发时对其进行更新。
button和标签的另一种方法是使用视图标签,正如我在这个答案中所描述的那样。
在cellButtonWasTapped:
或者你从button发送的任何动作)中,你可能要查找包含button的单元格的索引path。 我解释了如何在这个答案中做到这一点 。
-(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的标签
在发布SO之前,请做更好的研究。 这里是将Button添加到单元格的代码
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];