rbastic
rbastic

Reputation: 1

Put a UIButton inside a UITableView

I am trying to put a UIButton inside a UITableViewCell.

The UITableView also has cells containing UILabels and and the cells have gradient backgrounds being drawn into them. The problem I am seeing is that my UIButton is just a filled black cell with no text display, and changing both the color and background color properties does not appear to do anything.

Here's the code I use to create the button:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"InAppViewController, doing this - width= %f", self.wdth);
    static NSString *ProductTableIdentifier = @"ProductTableIdentifier";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ProductTableIdentifier];
    if (cell == nil)
    {
        if (self.wdth >= 700) {
            CGRect cellFrame = CGRectMake(0, 0, (self.wdth - 100), 40);
            cell = [[[UITableViewCell alloc] initWithFrame:cellFrame reuseIdentifier: ProductTableIdentifier] autorelease];

            // Set up some labels...

            CGRect seenValueRect = CGRectMake(self.wdth-320, 0, 100, 40);
            UIButton *seenValue = [UIButton buttonWithType:UIButtonTypeRoundedRect];
            [seenValue setBackgroundColor:[UIColor greenColor]];
            seenValue.tag = kSeenValueTag;
            seenValue.frame = seenValueRect;
            [seenValue setTitle:@"I'm a clone" forState:UIControlStateNormal];

            [cell.contentView addSubview:seenValue];

            [seenValue release];

            // Continue setting up cell...

The pastebin link contains the code to the entire cellForRowAtIndexPath() function (for the UITableView in question).

http://pastebin.com/bpEyfruE

Upvotes: 0

Views: 896

Answers (2)

albianto
albianto

Reputation: 4142

It can't even be that you have to do [cell addSubview:seenValue]; instead of [cell.contentView addSubview:seenValue];

You could also try to do CGRectMake(110, 11, 185, 30) instead of CGRectMake(self.wdth-320, 0, 100, 40): I use it and it works well but I don't know if it what you need

Upvotes: 0

Ruben Marin
Ruben Marin

Reputation: 1637

Maybe the problem is that you're releasing the UIButton innitialized with convenience initializer [UIButton buttonWithType:(UIButtonType)]. Try removing the [button release]

Upvotes: 2

Related Questions