我正在使用带有 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/