Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 模拟tableview行从tableview单元格中选择_Swift_Uitableview - Fatal编程技术网

Swift 模拟tableview行从tableview单元格中选择

Swift 模拟tableview行从tableview单元格中选择,swift,uitableview,Swift,Uitableview,我有一个应用程序,可以检查用户是否下载了某个特定的文件,如果他们没有提醒他们有机会下载该文件。此代码是从UITableViewCell中调用的,但我不确定如何使用tableView调用视图控制器来模拟按下第一行所需的文件始终位于第一行 下面是一段代码 if (fileLbl.text == baseMapDisplayname) { let alertController = UIAlertController(title: "Basemap Not Downloaded

我有一个应用程序,可以检查用户是否下载了某个特定的文件,如果他们没有提醒他们有机会下载该文件。此代码是从UITableViewCell中调用的,但我不确定如何使用tableView调用视图控制器来模拟按下第一行所需的文件始终位于第一行

下面是一段代码

if (fileLbl.text == baseMapDisplayname) {
            let alertController = UIAlertController(title: "Basemap Not Downloaded", message: "Please first download the Offline Basemap", preferredStyle: .alert)

            var rootViewController = UIApplication.shared.keyWindow?.rootViewController
            if let navigationController = rootViewController as? UINavigationController {
                rootViewController = navigationController.viewControllers.first
            }
            if let tabBarController = rootViewController as? UITabBarController {
                rootViewController = tabBarController.selectedViewController
            }

            alertController.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel   ,handler: nil))
            alertController.addAction(UIAlertAction(title: "Download", style: UIAlertActionStyle.default,handler: { (action: UIAlertAction!) in
                //TODO - Simulate action of selecting first row of tableview

               //this does not work
                let indexPath = IndexPath(row: 0, section: 0)
                MainVC().tableView.selectRow(at: indexPath, animated: true, scrollPosition: .bottom)
                MainVC().tableView.delegate?.tableView!(tableView, didSelectRowAt: indexPath)

            }))
            rootViewController?.present(alertController, animated: true, completion: nil)

        }
试试这个:

let indexPath = IndexPath(row: 0, section: 0);
self.tableView.selectRow(at: indexPath, animated: false, scrollPosition: UITableViewScrollPosition.none)
self.tableView(self.tableView, didSelectRowAt: indexPath)

您需要编写一个MainVC遵守的简单协议,它还需要包含一个函数来通知MainVC已按下下载按钮。大概是这样的:

protocol DownloadDelegate {
    func shouldDownloadFile()
}
因此,您需要通过创建一个变量(如var DownloadDelegate:DownloadDelegate?)将MainVC设置为弹出该警报的类中的DownloadDelegate,在下载操作中,您可以说:

self.downloadDelegate?.shouldDownloadFile()

这将通知MainVC,您可以通过执行self.tableView.selectRow。。。你已经计划好了。

在我看来,你正在创建MainVC的新实例,难道你不能使用self吗?如果您有合适的控制器实例,selectRow应该可以工作,而且当self已经是tableview委托时,您也不需要委托。这不起作用,因为tableview在UITableViewCell的上下文中不存在。这与表格单元格无关,而是通知拥有tableview的控制器行选择。如果您没有对MainVC实例的引用,可以使用上面建议的委托协议、扩展UIAlertController的回调块、通知通知中心、alertController中的presentingController等