简体   繁体   中英

Swift: table cell appears for a second and disappears

I have a parent UIView inside a UIViewController. 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. Since the controller is built under xib, I register the cells inside the 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(). the solution was -> to unhide cells in prepareForReuse(), prepareForReuse() is used inside cell class itself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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