简体   繁体   English

Swift:表格单元格出现一秒钟然后消失

[英]Swift: table cell appears for a second and disappears

I have a parent UIView inside a UIViewController.我在 UIViewController 中有一个父 UIView。 This parent UIView is put under segmented control such that when a different index is selected, this parent view brings to front the according added subviews (eg currentViewController().view). This parent UIView is put under segmented control such that when a different index is selected, this parent view brings to front the according added subviews (eg currentViewController().view).

var parentView: UIView!
var currentView: UIView!
override func viewDidLoad() {
    currentView = currentViewController().view
    parentUIView.addSubview(currentView)
    parentUIView.bringSubview(currentView)
}

There is a UITableView inside currentViewController. currentViewController 里面有一个 UITableView。 Since the controller is built under xib, I register the cells inside the currentViewController.由于控制器是在 xib 下构建的,所以我在 currentViewController 中注册了单元格。 (example code is the following) (示例代码如下)

import UIKit

class currentViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

@IBOutlet var stockInfo: UITableView!

override func viewDidLoad() {
    super.viewDidLoad()
    stockInfo.delegate = self
    stockInfo.dataSource = self
    stockInfo.register(UITableViewCell.self, forCellReuseIdentifier: "something")
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 2
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = stockInfo.dequeueReusableCell(withIdentifier: "something", for: indexPath) as! UITableViewCell
    cell.textLabel?.text = "testing DEFAULT"
    return cell
}

The code is very simple and straight forward.代码非常简单直接。 When segmented control is selected, I check the index and bring this view to the front.选择分段控件时,我检查索引并将此视图带到前面。 The above code is just an example.上面的代码只是一个例子。 I have other views too.我也有其他看法。

Unfortunately, my hard-coded cells (for debugging convenience) don't even show up for more than a second.不幸的是,我的硬编码单元(为了调试方便)甚至没有出现超过一秒钟。 They appear when the view is first loaded but then disappear in a second without any user actions.它们在第一次加载视图时出现,但在没有任何用户操作的情况下在一秒钟内消失。 I suppose they are deallocated?我想他们被释放了? Or is there any reason that I am unaware of?或者有什么我不知道的原因? Can someone help?有人可以帮忙吗? I have been stuck on it for hours.我已经坚持了几个小时。

let me add smth, i have wasted whole day to figure it out.让我补充一点,我已经浪费了一整天的时间来弄清楚。 my table view cells were disappearing after reloadData().我的表格视图单元格在 reloadData() 后消失了。 the solution was -> to unhide cells in prepareForReuse(), prepareForReuse() is used inside cell class itself.解决方案是 -> 在 prepareForReuse() 中取消隐藏单元格,prepareForReuse() 用于单元格类本身。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM