ios - UITableView 项目有时会在选择时消失(取决于屏幕尺寸)

标签 ios swift xcode uitableview

我正在使用 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,一切都会好起来的: enter image description here 但是如果我选择 UITableViewCell,里面的项目将会消失: enter image description here

这是我的 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/

相关文章:

iphone - 如何在动态标签后添加图片?

ios - 核心数据迁移: Could not cast value of type 'NSManagedObject_MyType' to 'MyModule.MyType'

ios - 如何引用 AppDelegate.swift 中的方法?

swift - RunLoop 与 DispatchQueue 作为调度器

ios - 创建都继承自 BaseViewController 的 TableViewController 和 UIViewController

objective-c - "Expected ' ; swift 下的 ' after top level declarator"

swift - 如何在 Xcode swift 中以编程方式更改 UIBarButtonItem 功能

ios - 在 CalloutAccessoryView 中更改 UIButton 的文本颜色

android - Firebase:报告分析和崩溃数据以分离项目

xcode - 如何在Xcode 9.3上将“快速打开”还原为黑暗主题?