【问题标题】:Activity indicator view in table cell表格单元格中的活动指示器视图
【发布时间】:2019-05-28 16:27:12
【问题描述】:

我有一张桌子,我想这样当我点击一个单元格时,活动指示器会围绕它旋转,而不是在一个难以理解的地方

看起来像这样。在代码中,我有 MainViewController 模块和 Presenter 模块,它们确定何时启动活动指示器。我有一个出口

@IBOutlet weak var activity: UIActivityIndicatorView! = UIActivityIndicatorView(style: .gray)

我在视图控制器中有两个函数可以启动和停止动画

func startActivity() {
   activity.startAnimating()
}


func stopActivity() {
   activity.stopAnimating()
}

有一个函数可以处理对单元格的点击

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
   tableView.deselectRow(at: indexPath, animated: true)
   output.callFromThePresenter(array: array[indexPath.row])

}

这个功能在presenter中实现。

func callFromThePresenter(array: String) {

  let userInteractiveQueue = DispatchQueue.global(qos: .userInteractive)

  async {
    DispatchQueue.main.async  {
        self.view.startActivity()
    }
    userInteractiveQueue.async {
         self.interactor.functionFromInteractor(data: array)
    }
  }
}

正如我所假设的,在视图控制器中,当您单击一个单元格时,callFromThePresenter() 函数将起作用,并且 Interactor 中的动画函数和数据传输函数将在其中启动,只要 Interactor 有完成这个函数,它会将数据返回给Presenter,在回调函数里面,我会运行stopActivity()函数。它一直有效,直到我决定设置定位

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
   tableView.deselectRow(at: indexPath, animated: true)
   output.callFromThePresenter(array: array[indexPath.row])
   let cell = tableView.cellForRow(at: indexPath)
   cell?.accessoryView = self.activity
}

只要我添加了这两行

let cell = tableView.cellForRow(at: indexPath)
cell?.accessoryView = self.activity

我打破了一切,就像我启动一样,我点击表格单元格,轮子在需要的地方转动,一切正常,但是在收到结果后,我再次戳了某个单元格,整个程序挂起,并且我不知道是什么原因。同时,函数计算出我能理解多少,因为到了控制台,函数启动并得到了一些结果,但是整个 UI 卡住了,我什么都做不了

【问题讨论】:

  • 看起来您有 一个 UIActivityIndicatorView 用于 多个 单元格。这行不通。并考虑修改didSelectRowAt 中的单元格不是持久的。细胞被重复使用。而且array: String的声明很混乱。
  • @vadian ,是的,我有一个,但问题是我不知道如何正确使用几个以及如何正确完成
  • 创建一个 custom 单元格,其中包含 UIActivityIndicatorView。将属性 isRunning 添加到您的数据模型中。根据isRunning 启动/停止cellForRow 中的指标视图。如果您的任务停止,请将 isRunning 设置为 false 并重新加载相应的行以停止指示器。
  • @vadian,不太明白,但我会尝试
  • 我用一个例子写了一个答案。

标签: swift uitableview uiactivityindicatorview


【解决方案1】:

简单示例:

  • 在 Interface Builder 中,将表格视图单元格的 Style 设置为 custom。
  • 将标签和活动指示器拖到单元格中,并根据需要设置适当的约束条件。
  • 选择活动指示器,按⌘⌥4(属性检查器)并检查停止时隐藏。
  • 创建自定义类,项目中UITableViewCell的子类

    class TitleCell: UITableViewCell {
    
        @IBOutlet weak var titleLabel: UILabel!
        @IBOutlet weak var activity: UIActivityIndicatorView!
    
        var isRunning : Bool = false {
            didSet {
                isRunning ? activity.startAnimating() : activity.stopAnimating()
            }
        }
    }
    
  • 在Interface Builder中设置原型单元的类为TitleCell,并将标签和指示器连接到IBOutlets

  • 创建一个至少具有title 和isRunning 属性的数据模型。

    struct Model {
        let title: String
        var isRunning : Bool
    }
    
  • 声明一个数据源数组

    var items = [Model]()
    
  • 在cellForRow 中更新 UI 元素(将标识符替换为您的真实标识符)

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! TitleCell
        let item = items[indexPath.row]
        cell.titleLabel!.text = item.title
        cell.isRunning = item.isRunning
        return cell
    }
    
  • 启动和停止指标实现didSelectRow

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: false)
        items[indexPath.row].isRunning.toggle()
        tableView.reloadRows(at: [indexPath], with: .none)
    }
    

要更改指标的状态,请始终使用模式在模型中设置 isRunning 并重新加载行。

【讨论】:

  • 我会建议覆盖 TitleCell 类中的 isSelected 以旋转指示器。 override var isSelected: Bool { didSet { if isSelected { } }}
  • @vadian,对这一行有点看不懂 cell.isRunning = item.isRunning 单元格无权访问 .isRunning 他甚至不知道它是什么
  • 请仔细阅读我的回答。自定义单元格TitleCell 有一个isRunning 属性,用于在更改时启动和停止指示器。
  • @vadian,让我问你点别的,我做了,正如你在示例中指出的那样,但我不太明白 didSelectRowAt () 函数中的一件事,它调用 tableView.reloadRows (在:[indexPath],与:.none)
  • @vadian,问题是仅通过单击表格单元格才会重新启动,在从函数 output.callFromThePresenter (数组:数组[indexPath.row]),如我在示例中
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多