简体   繁体   中英

Data pass tableview cell to tab bar view controller in Swift

I am new to swift

The tableview contain multiple images working fine this the code :

             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

Here I'm passing an image to a collection view but its not working:

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

                    navigationController?.pushViewController(tabcontroller, animated: true)

                }

After select any image in tableview cell image should show at tab bar with collection view. I am showing images and videos categories wise I can use tab bar controller

Data pass to:

Tableview ---> tab bar controller ===> 2 collection views

How can pass the tableview images data to collection using tab bar controller?

In your code

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

These codes do nothing is expected, because you declare an ImageController , but you don't use/present it.

So, according to the context, I assume that you have a UITabBarController in Main.storyboard , and the UITabBarController has two UIViewController , and one is ImageController . If I were right, you could try codes below:

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)

Another suggestion is you should use camel case to name your properties. For more details, you can refer to this style guideline .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM