Amon
Amon

Reputation: 109

Two uibutton shown in TableView Cell when device rotated

I am using a TableView with TableViewCell. I have programmatically added an ImageView, Text and a uibutton to the cell.The frame of the button is set different for portrait and landscape orientation. But when the device is rotated from portrait to landscape the are two buttons shown instead of one.

I tried removing button when in landscape mode button its not working.

switch ([indexPath section])
{

    case 0:
    {
        cell.imageView.image = [UIImage imageNamed:@"active.png"];
        cell.textLabel.text = @"Application Name";
        self.uninstallApplicationButton = [[UIButton alloc] init];
        self.uninstallApplicationButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [self.uninstallApplicationButton setTitle:@"Install" forState: UIControlStateNormal];
        [self.uninstallApplicationButton setBackgroundColor:[UIColor brownColor]];

        UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];

        if (device == UIUserInterfaceIdiomPhone)
        {
            if (UIInterfaceOrientationIsLandscape(orientation))
            {
               self.uninstallApplicationButton.frame = CGRectMake(490.0, 25.0, 65.0, 30.0);
            }
            else if(UIInterfaceOrientationIsPortrait(orientation))
            {
               self.uninstallApplicationButton.frame = CGRectMake(250.0, 25.0, 65.0, 30.0);

            }
        }

        else if(device == UIUserInterfaceIdiomPad)
        {
            self.uninstallApplicationButton.frame = CGRectMake(600.0, 25.0, 150.0, 30.0);
        }

    }
    [cell.contentView addSubview:uninstallApplicationButton];
        break;

Upvotes: 0

Views: 64

Answers (2)

Caleb
Caleb

Reputation: 125007

You said that the code is in your -cellForRow(at:) method. Since table views reuse their cells, your code has to consider that the cell it's configuring may have already been configured previously. A good approach is to avoid adding any buttons or other views to your cells in this method and just add them instead in your storyboard. If you must add views to your cells, you should either remove any that were added previously or just reuse them avoid adding them again.

Upvotes: 1

KKRocks
KKRocks

Reputation: 8322

You need to remove button from its superview before alloc init.

Upvotes: 0

Related Questions