2016-11-07 53 views
1

如何将已经在UITableview中使用的URL的解析值传递给已在每个UITableviews单元格中使用的UICollectionview(水平滚动)?我想用迅捷的语言来做到这一点。将值从uitableview传递到uicollectionview

我正在使用名称为“collectionCat”的模型。

import Foundation 

public class CollectionCat{ 



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



} 

我也在使用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() 

    } 



} 

我不知道什么是如何存储的数据传送到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 
} 

任何人请帮帮我吗?

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) 
    } 



} 
+0

你能包括从你的'SecondHomeViewCell'类的代码 – Danoram

+0

在您的SecondHomeViewCell中,通过storyboard添加集合视图@IBOutlet weak var collectionView:UICollectionView! ,cell.setData(collect [indexPath.row),并在setData中,将此数组设置为收集视图的数据模型 – jpulikkottil

回答

0

添加阵列在你的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 
} 

如果这不是你正在寻找的答案,请告诉我。因为我想澄清,但我没有评论的能力。我需要达到50个声望才能评论并提出一些澄清。

+0

刚才我已经添加了第二个HomeViewCell的代码。你能不能也看看,也给我一个答案,包括那个。 –

+0

你能否也请告诉我如何在SecondHomeViewCell中调用它 –

0

您可以通过在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) 
    } 



} 

//现在的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 
}