Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set Action for UIButton in UITableViewCell

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?

like image 717
Romowski Avatar asked Mar 27 '13 04:03

Romowski


3 Answers

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!!!

like image 84
Vishnu Avatar answered Nov 16 '22 06:11

Vishnu


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.

like image 35
rob mayoff Avatar answered Nov 16 '22 05:11

rob mayoff


-(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

like image 2
Peter Zhu Avatar answered Nov 16 '22 07:11

Peter Zhu