Reputation: 1548
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?
Upvotes: 15
Views: 36321
Reputation: 51
-(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
Upvotes: 2
Reputation: 2243
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!!!
Upvotes: 26
Reputation: 385500
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.
Upvotes: 15
Reputation: 479
Please do better research before posting on SO. Here is the code to add Button to cell
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];
Upvotes: -3