Adding dynamic buttons to uitableviewcell app - iPhone

I have a verb conjugation application that displays willow translations in the first cell of the table. Currently the translation list is just a string (a comma separated list), but I would like to change it to have buttons with buttons. I had a game around adding buttons to the cell view without much success, but my only experience using custom cells was using a certain positioning, so I'm not sure how to achieve a dynamic list of buttons (with different widths) inside the cell.

Any help is greatly appreciated.

Greetings.

+5
source share
4 answers

Do the following

-(CGRect)placeAButton:(NSString*)textFromField withCell:(UITableViewCell*)cell
{
    CGSize      theSize;
    CGSize      constraintSize;

    // Create a button and add as subview to cell
    // Get coordinates to place the button

    UIButton *button    = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];

    button.titleLabel.font              = [UIFont systemFontOfSize:MEDIUM_FONT_SIZE];;
    button.titleLabel.lineBreakMode     = UILineBreakModeTailTruncation;
    button.contentVerticalAlignment     = UIControlContentVerticalAlignmentCenter;
    button.contentHorizontalAlignment   = UIControlContentHorizontalAlignmentCenter;

    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button setTitle:textFromField forState:UIControlStateNormal];

    UIImage *buttonBkground;
    buttonBkground = [UIImage imageNamed:@"blueButton.png"];
    UIImage *newImage = [buttonBkground stretchableImageWithLeftCapWidth:12.0 topCapHeight:0.0];
    [button setBackgroundImage:newImage forState:UIControlStateNormal];
    [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [cell.contentView addSubview:button];

[self.tableView reloadData]
}

You will need to keep track of the width, height, etc. when creating buttons dynamically.

+4
source

I just did this for my new iPad app, so I thought I was embedding the code.

// NOTE - This is within a loop     
// Add the translation as a button
    UIButton *translationButton = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
    translationButton.backgroundColor = [UIColor clearColor];
    translationButton.titleLabel.font = [UIFont boldSystemFontOfSize:16];
    [translationButton setTitle:translation forState:UIControlStateNormal];
    [translationButton addTarget:self action:@selector(translationSearch:) forControlEvents:UIControlEventTouchUpInside];
    // Work out required size
    fontSize = [translationButton.titleLabel.text sizeWithFont:translationButton.titleLabel.font];      
    CGRect buttonFrame = CGRectMake(xOffset, 15, fontSize.width + 20.0, 24);
    [translationButton setFrame:buttonFrame];

    // Now set the new x Offset
    xOffset = buttonFrame.origin.x + buttonFrame.size.width + 6.0;

    [verbView addSubview:translationButton];    

Greetings.

+4
source

UIView. UIButton .

contentView.

-tableView:cellForRowAtIndexPath: ( ) , contentView.

, - , [tableView reloadData] , .

+3
source

You can do this inside the cellForRowAtIndexPath viewcontroller function:

var btn = UIButton(frame: CGRectMake(100, 100, 100, 20));
    btn.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal);
    btn.setTitle("My Button", forState:UIControlState.Normal);
    btn.addTarget(self, action: "buttonTapped:", forControlEvents: UIControlEvents.TouchUpInside);

    //add the buttons to the cell
    cell.contentView.addSubview(btn)

Add another function to the same class that will receive the event with the button pressed

func buttonTapped(sender: UIButton!)
{
    println("Button Tapped")
}
0
source

All Articles