【问题标题】:Tableview disappears when scrolling滚动时表格视图消失
【发布时间】:2021-10-02 17:36:02
【问题描述】:

我有一个在用户滚动时显示隐藏单元格的 tableView。不知道为什么会发生这种行为。

在 viewDidLoad() 中

    watchListTable = UITableView(frame: CGRect(x: self.view.frame.width * 0.25, y: 0, width: self.view.frame.width * 0.75, height: 300)) //height = 200
    watchListTable.isHidden = true
    watchListTableFrame = CGRect(x: self.view.frame.width * 0.25, y: 0, width: self.view.frame.width * 0.75, height: 300)
    watchListTableFrameHide = CGRect(x: self.view.frame.width * 0.25, y: 0, width: self.view.frame.width * 0.75, height: 0)
    watchListTable.register(UITableViewCell.self, forCellReuseIdentifier: "MyCell")
    watchListTable.register(UITableViewCell.self, forCellReuseIdentifier: "closeCell")

    watchListTable.dataSource = self
    watchListTable.delegate = self
    watchListTable.CheckInterfaceStyle()
    watchListTable.roundCorners(corners: .allCorners, radius: 8)
    watchListTable.backgroundColor = .systemGray6
    //remove the bottom line if there is only one option
    watchListTable.tableFooterView = UIView()

    view.addSubview(watchListTable)

一旦用户点击按钮,表格就会以动画方式展开。

   //watchlist won't animate properly on the initial setup. So we set it to be 
    hidden, then change the frame to be 0,  unhide it, and then animate it. Only will 
    be hidden on the initial setup.
    if(watchListTable.isHidden == true)
    {
        watchListTable.isHidden = false
        watchListTable.frame = watchListTableFrameHide
    }
    
    UIView().animateDropDown(dropDown: watchListTable, frames: 
    self.watchListTableFrame)
    watchListTable.reloadData()

在 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell

       if(indexPath.row >= watchListStocks.count)
        {
            let cell = tableView.dequeueReusableCell(withIdentifier: "closeCell", 
            for: indexPath as IndexPath)
            cell.selectionStyle = .none
            cell.textLabel?.text = indexPath.row == watchListStocks.count + 1 ? 
            "Close List" : "Create New Watchlist"
            cell.textLabel?.textColor = .stockOrbitTeal
            cell.textLabel?.textAlignment = .center
            cell.backgroundColor = .systemGray6
            cell.separatorInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 
            .greatestFiniteMagnitude)
           
            return cell                
        }
        else
        {
            let cell = tableView.dequeueReusableCell(withIdentifier: "MyCell", for: 
            indexPath as IndexPath)
            cell.selectionStyle = .none

            if(indexPath.row == 0)
            {
                cell.layer.cornerRadius = 8
                cell.layer.maskedCorners = [.layerMinXMinYCorner, 
                .layerMaxXMinYCorner]
            }
            else
            {
                cell.layer.cornerRadius = 8
                cell.layer.maskedCorners = [.layerMinXMaxYCorner, 
                .layerMaxXMaxYCorner]
                cell.separatorInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 
                .greatestFiniteMagnitude)
                cell.directionalLayoutMargins = .zero
            }
            
            let label = UITextView()
            
            label.frame = CGRect(x: 0, y: 0, width: cell.frame.width * 0.45, height: 
            cell.frame.height)
            label.text = watchListStocks[indexPath.row].listName
            label.textColor = .stockOrbitTeal
            label.textAlignment = .center
            label.font = UIFont.systemFont(ofSize: 18, weight: UIFont.Weight.medium)
            label.backgroundColor = .systemGray5
            label.delegate = self
            label.tag = indexPath.row
            cell.addSubview(label)
            
            cell.backgroundColor = .systemGray5
            cell.layer.cornerRadius = 8
            
            return cell
        }

当我滚动时,所有单元格都被隐藏了。我看到它们是在 cellForRowAt 中创建的,但是它们没有出现在我的屏幕上。为什么细胞被隐藏?我已经搜索了整个stackoverflow。

【问题讨论】:

  • 这可能完全无关紧要,但永远不要说cell.addSubview。您可以添加到contentView,而不是cell
  • 感谢您提供的信息!现在为此更改代码...您是否曾经遇到滚动时表格消失的情况?这对我来说太奇怪了,我似乎无法弄清楚为什么......
  • 表格在那里,我可以点击滚动的单元格。它只是不可见....

标签: ios swift uitableview


【解决方案1】:
  1. 您不应该在cellForRowAt 中添加子视图。当您调用dequeueReusableCell 时,首先它会创建新单元格,但是当您开始滚动时,它将开始返回之前关闭的单元格,这意味着它们已经有UITextView 子视图,并且您正在顶部添加一个那个。

  2. dequeueReusableCell 返回的单元格不必已经有最终大小,这就是为什么你不能使用cell.frame.width 来计算你的子视图大小,我认为这可能是你看不到它的原因。

您需要做的:创建一个UITableView 子类,如下所示:

class MyCell: UITableViewCell {
    let label = UITextView()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupCell()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        setupCell()
    }
    
    func setupCell() {
        label.textAlignment = .center
        label.font = UIFont.systemFont(ofSize: 18, weight: UIFont.Weight.medium)
        label.backgroundColor = .systemGray5
        contentView.addSubview(label)
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        label.frame = CGRect(x: 0, y: 0, width: contentView.frame.width * 0.45, height: contentView.frame.height)
    }
}

在这里,您只需在初始化期间添加一次子视图,并在每次更改单元格大小时更新标签框。不要忘记将此类添加到故事板中的单元格和let cell = tableView.dequeueReusableCell(withIdentifier: "MyCell", for: indexPath as IndexPath) as! MyCell,这样您就可以将委托设置为文本字段等。

如果这没有帮助,请查看 View Hierarchy 以了解实际情况

【讨论】:

  • 这是视图层次结构......它显示它正在显示,但它不是正在发生的事情。 imgur.com/a/6gQhmFI
  • 所以在花费了很多很多小时之后,我调用了 watchListTable.roundCorners(corners: .allCorners, radius: 8),这使得我滚动后视图隐藏了......不知道为什么跨度>
  • @Chris 没有注意到那一行。完全不知道是什么,不是系统功能,应该是扩展
  • 是的,它是 UIView 的扩展。不知道为什么它会导致视图隐藏,虽然哈哈。
【解决方案2】:

所以几个小时后,我想通了......

我在 viewDidLoad() 中调用了这个函数

watchListTable.roundCorners(corners: .allCorners, radius: 8)

这让我在滚动后隐藏了我的表格。我删除了这行代码,现在滚动时表格完全可见。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-04-26
    • 1970-01-01
    • 2020-02-04
    • 1970-01-01
    • 1970-01-01
    • 2016-09-21
    • 1970-01-01
    相关资源
    最近更新 更多