ios - 设备旋转时 TableView Cell 中显示的两个 uibutton

标签 ios objective-c uitableview uibutton uiinterfaceorientation

我正在使用带有 TableViewCell 的 TableView。我以编程方式向单元格添加了一个 ImageView、文本和一个 uibutton。按钮的框架针对纵向和横向方向设置了不同。但是当设备从纵向旋转到横向时,会显示两个按钮而不是一个。

我尝试在横向模式按钮不起作用时删除按钮。

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;

最佳答案

您说代码位于您的 -cellForRow(at:) 方法中。由于 TableView 重用其单元格,因此您的代码必须考虑它正在配置的单元格可能之前已经配置过。一个好的方法是避免在此方法中向单元格添加任何按钮或其他 View ,而只需将它们添加到 Storyboard中。如果您必须向单元格添加 View ,则应该删除之前添加的任何 View ,或者只是重复使用它们,避免再次添加它们。

关于ios - 设备旋转时 TableView Cell 中显示的两个 uibutton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40085851/

相关文章:

iphone - 如何制作一个圆形的UIPickerView (iPhone/iPad)?

objective-c - 在 Windows 上使用 objective-c 进行 GUI 编程?

ios - swift3 中的表格 View 单元格

ios - 带有自定义 UITableViewCell 的 UITableView 中的 UITextField 中的 UIDatePicker : realtime update

ios - NSDateFormatter 中的 AM/PM 问题

ios - 如何在swift5中从WKWebviews预加载WKWebviews

ios - self.presentingViewController 在 iOS 8 上损坏?

ios - 使用 ifndef 和 || 进行条件编译没有捕获第二种情况

ios - 如何过滤在 Core Data 中有对象的 TableView ?

php - 如何使用 AFNetworking 2.0 检查 Wordpress 登录信息?