【问题标题】:Segue lag, tableview in SwiftSegue 滞后,Swift 中的表格视图
【发布时间】:2018-03-29 14:12:01
【问题描述】:

我在 Swift 3 中的第一个应用程序中工作。我正在使用 tableView(通过“扩展 MainController:UITableViewDataSource”)。从这个 tableView 中,通过情节提要,我有两个 segues。一个用于编辑(通过单击附件图标),第二个用于更多详细信息屏幕(通过单击表格行)。我不是通过代码来调用这个 segues,而是通过情节提要。

我的问题是有时会有很大的滞后。就像单击一行后一样,下一个屏幕会在 30 秒后显示。但现在总是。有时它会立即工作。有趣的是,当我触摸第 1 行时,没有任何反应,接下来我单击第 2 行,然后出现第 1 行。

我也在用delegate,这是准备segues的代码:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    // 1
    if segue.identifier == "AddSensor" {
        // 2
        let navigationController = segue.destination
            as! UINavigationController
        // 3
        let controller = navigationController.topViewController
            as! AddController
        // 4
        controller.delegate = self
    }
    else if segue.identifier == "EditSensor" {
        let navigationController = segue.destination
            as! UINavigationController
        let controller = navigationController.topViewController
            as! AddController
        controller.delegate = self
        if let indexPath = tableView.indexPath(
            for: sender as! UITableViewCell) {
            controller.sensorToEdit = sensors[indexPath.row]
        }
    }
    else if segue.identifier == "DetailSeq" {
        let navigationController = segue.destination
            as! UINavigationController
        let controller = navigationController.topViewController
            as! DetailController
        controller.delegate = self
        if let indexPath = tableView.indexPath(
            for: sender as! UITableViewCell) {
            controller.sensorRecieved = sensors[indexPath.row]
        }
    }

}

我读到这是iOS8中的常见错误,可以通过添加来解决

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {  
DispatchQueue.main.async {
    self.performSegue(withIdentifier: "DetailSeq",sender: self)
}
}  

但这对我不起作用。我不知道接下来我应该做什么来解决这个问题。谁能指导我?

【问题讨论】:

  • 您是否有此工作没有滞后,然后开始看到滞后?如果是这样,你改变了什么?如果你从来没有开始工作,你可能想从小处着手,然后一步一步地去......尝试使用普通视图控制器 - 没有委托,没有传递数据等。一旦你开始工作,然后开始添加其他部分。
  • 它被称为Segue,而不是Seque。 G,没有 Q。
  • 这可能值得检查。您能否确保您已实施“didSelectRowAtIndexPath”而不是“didDeSelectRowAtIndexPath”强调“取消选择”,过去曾有过我实施“取消选择”的实例,这将导致转场仅在取消选择时触发。另外,您可以发布更多代码,

标签: ios uitableview


【解决方案1】:

根据SO question,如果您在代码中而不是在情节提要中使用 segue,则可以修复您的错误。像这样,你的目的地是你想去的 VC。

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: 
    NSIndexPath) {

    DispatchQueue.main.async {
        self.presentViewController(destination, animated: true) { () -> Void                    
              in

        }
    }
}

【讨论】:

    【解决方案2】:

    你必须像这样使用:

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        DispatchQueue.main.async {
            self.performSegue(withIdentifier: "DetailSeq",sender: self)
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多