我有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];
Run Code Online (Sandbox Code Playgroud)
在我的TimerCell中,我有两个UILabel和一个UIButton.对于这个按钮,我想设置一些方法的动作.
我怎样才能做到这一点?以及如何UILabel实时显示来自我的背景倒数计时器的数据?
Dan*_*any 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);
}
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助!!!
rob*_*off 16
由于您有一个UITableViewCellnamed 的子类TimerCell,因此可以添加outlet属性TimerCell.例如:
@interface TimerCell : UITableViewCell
@property (nonatomic, strong) UIButton *button;
@property (nonatomic, strong) UILabel *label;
@end
Run Code Online (Sandbox Code Playgroud)
在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];;
Run Code Online (Sandbox Code Playgroud)
您可以使用其label属性访问单元格的标签,以便在计时器触发时更新它.
另一种获取按钮和标签的方法是使用视图标签,正如我在本回答中所述.
在cellButtonWasTapped:(或从按钮发送的任何操作)中,您可能希望查找包含按钮的单元格的索引路径.我在这个答案中解释了如何做到这一点.
| 归档时间: |
|
| 查看次数: |
34878 次 |
| 最近记录: |