我正在使用 Swift 5 和 Xcode 11 创建一个带有 3 个独立 View Controller (2 个常规 View 、1 个表格 View )的选项卡式应用程序。在我的第三个 View (即带有表格 View 的 View )内,以及在我的UITableViewCells,有 1 个按钮,颜色为红色。我在两台测试设备上测试了我的程序,一台具有较大的屏幕,另一台具有较小的 iPhone 5s 屏幕,以下是我得到的结果:
更大屏幕的iPhone(iPhone 6 Plus): 一切正常,即使我选择 UITableViewCell,里面的项目也不会消失
iPhone 5s: 如果我不选择 UITableViewCell,一切都会好起来的: 但是如果我选择 UITableViewCell,里面的项目将会消失:这是我的 uitableviewcontroller:
import UIKit
@objcMembers class CustomViewController: UITableViewController {
var tag = 0
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidAppear(_ animated: Bool) {
}
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
// 3
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
tag = tag + 1
let cell = tableView.dequeueReusableCell(withIdentifier: "themeCell", for: indexPath) as! ThemeCell
/////////
let cellButton = UIButton(frame: CGRect(x: 0, y: 5, width: 50, height: 30))
cellButton.translatesAutoresizingMaskIntoConstraints = false
cell.addSubview(cellButton)
cell.accessoryView = cellButton
cellButton.backgroundColor = UIColor.red
cellButton.leadingAnchor.constraint(equalTo: cell.leadingAnchor, constant: 10).isActive = true
cellButton.topAnchor.constraint(equalTo: cell.topAnchor, constant: 5).isActive = true
cellButton.widthAnchor.constraint(equalToConstant: 50).isActive = true
cellButton.heightAnchor.constraint(equalToConstant: 30).isActive = true
cell.img.image = UIImage(named: SingletonViewController.themes[indexPath.row])
cell.accessoryView = cellButton
cellButton.backgroundColor = UIColor.red
cellButton.addTarget(self, action: #selector(CustomViewController.backBTN(sender:)), for: .touchUpInside)
cellButton.tag = tag
return cell
}
}
知道为什么会发生这种情况吗?
最佳答案
删除该行代码
cell.accessoryView = cellButton
将按钮添加为 subview 或将其设置为附件 View 。
关于ios - UITableView 项目有时会在选择时消失(取决于屏幕尺寸),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59828220/