I have XIB
file TimerCell.xib with UITableViewCell
. In other class in cellForRowAtIndexPath I initialize this 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];
In my TimerCell I have two UILabel
and one UIButton
. For this button I would like to set action to some method.
How can I do that? And how to show in the first UILabel
the data from my background countdown timer in real time?
This piece of code will help you
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);
}
Hope this helps!!!
Since you have a subclass of UITableViewCell
named TimerCell
, you can add outlet properties to TimerCell
. For example:
@interface TimerCell : UITableViewCell
@property (nonatomic, strong) UIButton *button;
@property (nonatomic, strong) UILabel *label;
@end
In TimerCell.xib
, connect the outlets to the button and the label.
Then, in tableView:cellForRowAtIndexPath:
, you can easily access the button to set its target and action:
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];;
You can access the cell's label using its label
property to update it when the timer fires.
Another way to get at the button and the label is to use view tags, as I described in this answer.
In cellButtonWasTapped:
(or whatever action you send from the button), you'll probably want to look up the index path of the cell containing the button. I explain how to do that in this answer.
-(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];
}
And set UIButton and UILabel's tag in your XIB file TimerCell.xib
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With