Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a clickable button on table cell?

I am adding a button in table view cell dynamically. Button shown on table but I am not able to click on them. Here is my Code,

This my tableviewcell class code:

MessageTableViewCell.h

#import <UIKit/UIKit.h>
@interface MessageTableViewCell : UITableViewCell {
{IBOutlet UIButton *chat_pic_btn;}}
@property (nonatomic, retain) UIButton *chat_pic_btn;
@end;

MessageTableViewCell.m

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {chat_pic_btn=[[UIButton alloc]init];
[self.contentView addSubview:chat_pic_btn];}}

MessageTable.m

-(void)customActionPressed :(id)sender
{
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Custom Button Pressed" 
                                                        message:[NSString stringWithFormat: @"You pressed the custom button on cell"]  
                                                       delegate:self cancelButtonTitle:@"Great" 
                                              otherButtonTitles:nil];
    [alertView show];
}

- (UITableViewCell *)tableView:(UITableView *)myTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"CellIdentifier";
    MessageTableViewCell *cell = (MessageTableViewCell *)[myTableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        // cell = [[MessageTableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier];
        cell = [[MessageTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
cell.chat_pic_btn.frame = CGRectMake(180, 24, 70,35);
            [cell.chat_pic_btn setImage:[UIImage imageNamed:@"done.png"]forState:UIControlStateNormal];
            [cell.chat_pic_btn addTarget:self action:@selector(customActionPressed:) forControlEvents:UIControlEventTouchDown];
return cell;
}

Please help me out. Thanks.

like image 718
Saurabh Avatar asked Sep 24 '12 05:09

Saurabh


People also ask

How do I add a row to a table in button click?

The insertRow() method creates an empty <tr> element and adds it to a table. The insertRow() method inserts the new row(s) at the specified index in the table. Note: A <tr> element must contain one or more <th> or <td> elements. Tip: Use the deleteRow() method to remove a row.


1 Answers

I suggest you delete your Button outlet in your TableViewCell. and just create your button dynamically in cellForRowAtIndexPath: I created a SampleCell Class, subclass of UITableViewCell, it has a UILabel outlet i called "lbl." This should work on your code, assuming that your selector is on the same class where you put your tablview;

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"SampleCell";
    SampleCell *cell = (SampleCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        NSArray* topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"SampleCell" owner:self options:nil];
        for (id currentObject in topLevelObjects) {
            if ([currentObject isKindOfClass:[UITableViewCell class]]) {
                cell = (SampleCell *)currentObject;
                break;
            }
        }
    }
    // Configure the cell.
    cell.lbl.text = @"Hello";
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    //set the position of the button
    button.frame = CGRectMake(cell.frame.origin.x + 100, cell.frame.origin.y + 20, 100, 30);
    [button setTitle:@"World" forState:UIControlStateNormal];   
    [button addTarget:self action:@selector(customActionPressed:) forControlEvents:UIControlEventTouchUpInside];
    button.backgroundColor= [UIColor clearColor];
    [cell.contentView addSubview:button];

    return cell;
}
like image 110
Jenn Eve Avatar answered Sep 28 '22 15:09

Jenn Eve