簡體   English   中英

Swift:表格單元格出現一秒鍾然后消失

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

我在 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).

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

currentViewController 里面有一個 UITableView。 由於控制器是在 xib 下構建的,所以我在 currentViewController 中注冊了單元格。 (示例代碼如下)

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
}

代碼非常簡單直接。 選擇分段控件時,我檢查索引並將此視圖帶到前面。 上面的代碼只是一個例子。 我也有其他看法。

不幸的是,我的硬編碼單元(為了調試方便)甚至沒有出現超過一秒鍾。 它們在第一次加載視圖時出現,但在沒有任何用戶操作的情況下在一秒鍾內消失。 我想他們被釋放了? 或者有什么我不知道的原因? 有人可以幫忙嗎? 我已經堅持了幾個小時。

讓我補充一點,我已經浪費了一整天的時間來弄清楚。 我的表格視圖單元格在 reloadData() 后消失了。 解決方案是 -> 在 prepareForReuse() 中取消隱藏單元格,prepareForReuse() 用於單元格類本身。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM