ios - 如何在 UI 工作中显示微调器?

标签 ios swift uitableview asynchronous spinner

我在 iOS 上有以下函数在 TableView 上执行 UI 工作,挑战是如果它开始花费比 0.5 秒更长的时间。应向用户显示微调器,以使屏幕看起来不会像卡住一样。

func updateForm(with rowItems: [RowItem]) {

    self.tableView.beginUpdates()   // performance tweak.

    let viewControllerName = String.init(describing: self.classForCoder)    // id
    var defaultSection = Form.createSectionWith(tag: viewControllerName, in: form)

    // MARK: - update rows

    let allRows = self.form.allRows

    let startTime = CFAbsoluteTimeGetCurrent()
    var showSpinner = false

    for (index, item) in rowItems.enumerated() {
        .
        .
        .
        <TableView processing work on 100's of rows>
        .
        .
        .

        // Evaluate our running time for this process loop, and display spinner if we're past a threshold of seconds.
        let timeElapsed = CFAbsoluteTimeGetCurrent() - startTime
        if timeElapsed > 0.5 && showSpinner == false {
            showSpinner = true
            self.showActivityIndicator(withStatus: "processing")    // {NEVER GETS DISPLAYED}
        }

    }  // for (index, item) ...

当然,当我调用 showActivityIndi​​cator 时,它从未真正显示出来。

如何中断和暂停 UI 工作以显示 showActivityIndi​​cator 微调器动画,然后让循环继续?

最佳答案

您需要离开主线程,以便让运行循环循环并发生重绘时刻。在继续您的工作之前显示微调器之后的简单延迟将解决问题。但是您将不得不重新组织代码以允许这样做。最简单的解决方案是放弃 0.5 秒的想法,在开始之前只显示微调器。

关于ios - 如何在 UI 工作中显示微调器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58547552/

相关文章:

swift - 将方法传递给函数时避免强引用

Xcode6 Beta7 无法使用?

ios - 如何使用搜索栏过滤字典数组

ios - 根据底层内容更改 StatusBar 文本颜色

ios - NSProgress 实现大量内存增长

ios - 从自定义 UIView 呈现 View Controller (xib 文件)

ios - 从 UIPickerView 接收输入时如何更新 UITextField?

ios - Swift UITableView 索引超出范围错误

ios - 问 : SearchBar Tableview with JSON File

ios - 更改音量和打开/关闭静音模式