gpt4 book ai didi

ios - 从 TableViewController 滞后呈现模态 ViewController

转载 作者:行者123 更新时间:2023-11-29 00:46:17 26 4
gpt4 key购买 nike

我有 UITableViewController,它需要在点击单元格时以模态方式呈现 View Controller 。为此,我正在使用 didSelectRowAt

模态视图 Controller 是一个自定义的 UIViewController 子类,它有一个 loadFromStoryboard() 类方法来加载它。

有时当我点击单元格时, View Controller 会快速呈现而不会出现问题。然而,有时它不会显示,例如,我尝试在 tableview 上滚动或点击另一个单元格。

我猜这是某种线程问题。但是,在我的整个应用程序中,我从来没有将任务委托(delegate)给另一个线程或启动另一个队列。

注意:我使用的是 Swift 3。

更新

这是 loadFromStoryboard() 方法:

class func loadFromStoryboard(particle: ParticleProtocol, isFavourite: Bool = false) -> ParticleDisplayViewController {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
if let viewController = storyboard.instantiateViewController(withIdentifier: "ParticleDisplayViewController") as? ParticleDisplayViewController {

viewController.particle = particle
viewController.isFavourite = isFavourite

return viewController
} else {
fatalError("Can't find ParticleDisplayViewController in Main storyboard")
}
}

这是来 self 的 UITableViewController 的 didSelectRowAt:

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
switch indexPath.section {
case 1:
let isFavourite = ParticleStorageManager.standardModelParticleIsFavourite(index: indexPath.row)
self.present(ParticleDisplayViewController.loadFromStoryboard(particle: standardModelParticles[indexPath.row], isFavourite: isFavourite), animated: true, completion: nil)
case 0:
let isFavourite = ParticleStorageManager.savedParticleIsFavourite(index: indexPath.row)
self.present(ParticleDisplayViewController.loadFromStoryboard(particle: self.particles[indexPath.row], isFavourite: isFavourite), animated: true, completion: nil)
default:
self.contextKey = "AddingParticle"
self.present(CreateParticleTableViewController.loadFromStoryboard(reciever: self), animated: true, completion: nil)
}

}

ParicleStorageManager 只是读取数据并将数据写入 UserDefaults

这是来自模态视图 Controller 的 viewDidLoad():

override func viewDidLoad() {
self.view.backgroundColor = .clear()

self.particleViewContainer.layoutIfNeeded()
self.particleViewContainer.addSubview(ParticleView(position: CGPoint.zero, width: particleViewContainer.width, particle: self.particle, isFavourite: self.isFavourite))

propertiesTableView.backgroundColor = UIColor.white().withAlphaComponent(0.3)
propertiesTableView.layer.borderWidth = 1.5
propertiesTableView.layer.borderColor = UIColor.black().cgColor
self.propertiesTableView.blur()
}

最佳答案

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) 中调用 self.present 是这里的原因,因为 UIPresentationController 可能花时间从 tableview 行选择中找到布局,并导致以模态方式呈现新 Controller 的延迟。

更好的呈现方式是在主线程上使用 DispatchQueue。这样可以避免延迟。

 DispatchQueue.main.async {
//Your Presentation code should be called here....
}

关于ios - 从 TableViewController 滞后呈现模态 ViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38571924/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com