Table 在 TableView with Custom Cell using Subview 中滚动时内容消失 - Swift

Table Content disappears on Scroll in TableView with Custom Cell using Subview - Swift

我有一个 ViewController,它使用多个子视图(HomeViewController 等),可以通过我的应用程序底部的自定义标签栏进行选择。在 HomeViewController 中有一个 UIView,其中包含一个 UITableView,其中包含一个带有名称和图像的原型自定义单元格。

import UIKit

class HomeViewController: UIViewController {

@IBOutlet weak var friendView: UITableView!

let friends = ["batman", "harsh", "ava", "sasha", "fatima", "alfred"]

override func viewDidLoad() {
    super.viewDidLoad()
    friendView.delegate = self
    friendView.dataSource = self
    friendView.allowsSelection = false
}
}

extension HomeViewController: UITableViewDelegate, UITableViewDataSource {

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 120
}

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

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = friendView.dequeueReusableCell(withIdentifier: "customCell") as! CustomCell
    let friend = friends[indexPath.row]
    cell.avatarImg.image = UIImage(named: friend)
    cell.nameLbl.text = friend
    return cell
}
}

自定义单元格:

import UIKit

class CustomCell: UITableViewCell {

@IBOutlet weak var friendView: UIView!
@IBOutlet weak var nameLbl: UILabel!
@IBOutlet weak var avatarImg: UIImageView!

override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
}

override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    // Configure the view for the selected state
}
}

当我启动应用程序时,一切看起来都很好。但是,当我开始在 table 内滚动时,所有数据突然消失了。故事板和代码之间的所有关系都应该很好。我认为这可能与我使用子视图的需要有关。

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var tabBarView: UIView!
@IBOutlet weak var contentView: UIView!

override func viewDidLoad() {
    super.viewDidLoad()
    
    Design.makeCornersRound(view: tabBarView, radius: 10.0)
    
    Timer.scheduledTimer(withTimeInterval: 0.1, repeats: false) { (timer) in
        self.switchToHomeViewController()
    }
}

@IBAction func onClickTabBar(_ sender: UIButton) {
    let tag = sender.tag
    
    if tag == 1 {
        switchToIncomingsViewController()
    }
    else if tag == 2 {
        switchToSpendingsViewController()
    }
    else if tag == 3 {
        switchToHomeViewController()
    }
    else if tag == 4 {
        switchToSavingsViewController()
    }
    else if tag == 5 {
        switchToSettingsViewController()
    }
}

func switchToHomeViewController() {
    guard let Home = self.storyboard?.instantiateViewController(withIdentifier: "HomeViewController") as? HomeViewController else { return }
    contentView.addSubview(Home.view)
    Home.didMove(toParent: self)
}
...
}

参考我一直在努力实现的教程:https://www.youtube.com/watch?v=ON3Z0PXSoVk

在这个函数中:

func switchToHomeViewController() {
    // 1
    guard let Home = self.storyboard?.instantiateViewController(withIdentifier: "HomeViewController") as? HomeViewController else { return }
    // 2
    contentView.addSubview(Home.view)
    // 3
    Home.didMove(toParent: self)
    // 4
}
  • 1 你创建了一个 HomeViewController
  • 的实例
  • 2 您将其视图添加到 cotentView
  • 3 你调用了 didMove() ...但是那没有做任何事情因为你还没有添加控制器到你的层级
  • 4 您的 Home 实例消失了,因此该控制器中的代码不再存在

您需要将控制器添加为 控制器。

作为旁注,变量名使用小写字母:

func switchToHomeViewController() {
    // create an instance of HomeViewController
    guard let homeVC = self.storyboard?.instantiateViewController(withIdentifier: "HomeViewController") as? HomeViewController else { return }
    // add it as a child view controller
    self.addChild(homeVC)
    // add its view
    contentView.addSubview(homeVC.view)
    // here you should either set the view's frame or add constraints
    //  such as:
    homeVC.view.frame = contentView.bounds
    // inform the controller that it moved to a parent controller
    homeVC.didMove(toParent: self)
}