简体   繁体   English

将值从uitableview传递到uicollectionview

[英]passing values from uitableview to uicollectionview

How can I pass parsed values from a url which has been used in an UITableview to a UICollectionview(scrolled horizontally) which has been used in each of the UITableviews cell? 如何将已在UITableview中使用的url中的已解析值传递给UICollection视图(在每个UITableviews单元格中使用过的UICollectionview(水平滚动)? I want do this in swift language. 我想用快速语言来做这件事。

I am using a model with the name "collectionCat". 我正在使用名为“collectionCat”的模型。

import Foundation

public class CollectionCat{



    var brandid:String = ""
    var brandname:String = ""
    var result:String = ""
    var noitems:String = ""



}

I am also using Alamofire to parse data from url. 我也在使用Alamofire来解析来自url的数据。

override func viewDidLoad() {
    super.viewDidLoad()

    self.startActivityAnimating(message: "", type: NVActivityIndicatorType.BallClipRotate, color: AppColor.AppRed, padding: 10)
    Alamofire.request(.POST, "http://goringr.co.in/Mathrubhumi_project_ios/brand.php", parameters: ["": ""])
        .validate()
        .responseJSON { response in
            switch response.result {
            case .Success:
                print("Validation Successful")
                self.jsonResultParse(response.result.value!)
                self.stopActivityAnimating()
            case .Failure(let error):
                print(error)
                self.stopActivityAnimating()
                // return userCatagory
            }


    }


    // Do any additional setup after loading the view.
}
  if JSONArray.count != 0 {
        //_ = [UserCatagory]()


        for i:Int in 0 ..< JSONArray.count  {

            let jObject = JSONArray[i] as! NSDictionary

            let uCollect:CollectionCat = CollectionCat()
            uCollect.brandid = (jObject["brandid"] as AnyObject? as? String) ?? ""
            uCollect.brandname = (jObject["brandname"] as AnyObject? as? String) ?? ""
            uCollect.result = (jObject["result"] as AnyObject? as? String) ?? ""
            uCollect.noitems = (jObject["noitems"] as AnyObject? as? String) ?? ""

            collect.append(uCollect)
            print("collect COUNT ",collect.count)
        }

        self.newTable.reloadData()

    }



}

What i dont know is how to transfer the stored data to UICollection view which is in each cell of the mentioned UITableview. 我不知道的是如何将存储的数据传输到UICollection视图,该视图位于上述UITableview的每个单元格中。

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
   // collectMaster = collect[indexPath.row]

    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as!  SecondHomeViewCell

   // collectMaster = CollectionCat()
    cell.get = ????

    return cell
}

Can anyone Please help me with this? 任何人都可以帮我这个吗?

The SecondHomeViewCell's code goes like this. SecondHomeViewCell的代码是这样的。

import UIKit
import Kingfisher
import Alamofire
import NVActivityIndicatorView



class SecondHomeViewCell: UITableViewCell{

    @IBOutlet weak var collectionView: UICollectionView!



    var genre:CollectionCat? = nil{
    didSet {
                collectionView.reloadData()

            }

    }




     func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        //return genre!.brandid

       // return genre
        return 1
    }

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("secondHome", forIndexPath: indexPath) as! SecondHomeCollectionViewCell

             if let genre = genre {
            cell.genre = genre.brandid[indexPath.row]

        }

        return cell
    }

    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
        let itemsPerRow:CGFloat = 4
        let hardCodedPadding:CGFloat = 5
        let itemWidth = (collectionView.bounds.width / itemsPerRow) - hardCodedPadding
        let itemHeight = collectionView.bounds.height - (2 * hardCodedPadding)
        return CGSize(width: itemWidth, height: itemHeight)
    }



}

Add a array in your ViewController, call it 在ViewController中添加一个数组,调用它

var collCat = [CollectionCat]()


func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
   let collectMaster = collCat[indexPath.row]

    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as!  SecondHomeViewCell


    cell.brandID = collectMaster.brandid
    //and so on

    return cell
}

If this is not the answer you are looking for, please tell me. 如果这不是您要找的答案,请告诉我。 Because I wanted to clarify but I don't have the ability to comment yet. 因为我想澄清,但我还没有能力发表评论。 I need to reach 50 reputation to be able to comment and ask some clarifications. 我需要达到50个声誉才能发表评论并提出一些澄清。

You can pass data to collection view by creating function in UITableviewCell custom class 您可以通过在UITableviewCell自定义类中创建函数将数据传递到集合视图

class SecondHomeViewCell: UITableViewCell{
//Array containing modals for particular tableview cell index path  


 var arrDataFromViewController = NSArray()
    func getData(arrData:NSArray){
        arrDataFromViewController = arrData
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.reloadData()
        }


    @IBOutlet weak var collectionView: UICollectionView!



    var genre:CollectionCat? = nil{
    didSet {
                collectionView.reloadData()

            }

    }




     func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        //return genre!.brandid

       // return genre
        return 1
    }

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("secondHome", forIndexPath: indexPath) as! SecondHomeCollectionViewCell

             if let genre = genre {
            cell.genre = genre.brandid[indexPath.row]

        }

        return cell
    }

    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
        let itemsPerRow:CGFloat = 4
        let hardCodedPadding:CGFloat = 5
        let itemWidth = (collectionView.bounds.width / itemsPerRow) - hardCodedPadding
        let itemHeight = collectionView.bounds.height - (2 * hardCodedPadding)
        return CGSize(width: itemWidth, height: itemHeight)
    }



}

//Now in tableview data source method //现在在tableview数据源方法中

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
   // collectMaster = collect[indexPath.row]

    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as!  SecondHomeViewCell

   // collectMaster = CollectionCat()
    cell.getData =  yourArray[indexPath.row]//Get Array data

    return cell
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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