数据将tableview单元格传递给Swift中的标签栏视图控制器

时间:2017-08-09 12:31:32

标签: ios swift uitableview swift3

我是swift的新手

tableview包含多个图像正常运行代码:

             override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

                    let cell = tableView.dequeueReusableCell(withIdentifier: "Media", for: indexPath)as! MediaCustomTableViewCell               
                    let row = indexPath.row                       
                    let media = Mediainfo[row] as MediaEvent                
                    cell.DisplayDate.text = media.date                
                    cell.DisplayName.text = media.eventName                       
                    cell.selectionStyle = .none                        
                    cell.DisplayImage.downloadImageFrom(link:media.bannerImages, contentMode: UIViewContentMode.scaleAspectFit)

                    return cell
                }


                override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
                    return CGFloat.leastNormalMagnitude
                }


                override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
                {                    
                    let media = Mediainfo[(indexPath.row)] as MediaEvent                        
                    let ImageStoryboard = UIStoryboard(name: "Main", bundle: nil)                      
                    let  ImageController = ImageStoryboard.instantiateViewController(withIdentifier: "IMAGESCOL")as!ImagesCollectionViewController

这里我将图片传递到集合视图但它不起作用:

                    ImageController.RecivedData1 = media.bannerImages                                            
                    let storyboard = UIStoryboard(name: "Main", bundle: nil)                      
                    let tabcontroller = storyboard.instantiateViewController(withIdentifier: "IMAGEVID") as! UITabBarController

                    navigationController?.pushViewController(tabcontroller, animated: true)

                }

在tableview中选择任何图像后,单元格图像应显示在带有集合视图的选项卡栏中。我正在显示图像和视频类别,我可以使用标签栏控制器

数据传递给:

Tableview --->标签栏控制器===> 2个集合视图

如何使用标签栏控制器将tableview图像数据传递给集合?

1 个答案:

答案 0 :(得分:1)

在您的代码中

let ImageStoryboard = UIStoryboard(name: "Main", bundle: nil)
let ImageController = ImageStoryboard.instantiateViewController(withIdentifier: "IMAGESCOL") as! ImagesCollectionViewController
ImageController.RecivedData1 = media.bannerImages

这些代码不会执行任何操作,因为您声明了ImageController,但您不使用/显示它。

因此,根据上下文,我假设UITabBarController中有Main.storyboard,而UITabBarController有两个UIViewController,其中一个是ImageController }。如果我是对的,你可以尝试以下代码:

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let tabBarController = storyboard.instantiateViewController(withIdentifier: "IMAGEVID") as! UITabBarController

// You should access the `imageController` through your `tabBarController`, 
// not instantiate it from storyboard.
if let viewControllers = tabBarController.viewControllers, 
   let imageController = viewControllers.first as? ImageController {
    imageController.recivedData1 = media.bannerImages
}

navigationController?.pushViewController(tabBarController, animated: true)

另一个建议是你应该使用驼峰案例来命名你的属性。有关详细信息,请参阅this style guideline