2016-11-07 14 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() 

    } 



} 

は言及のUITableViewの各セルにあるUICollectionビューに格納されたデータを転送する方法です。

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'クラスからのコードを含めることができます//

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

のUITableViewCellのカスタムクラスで関数を作成することによって、コレクションビューにデータを渡すことができます – Danoram

+0

SecondHomeViewCellで、storyboard経由でコレクションビューを追加してください。@IBOutlet weak var collectionView:UICollectionView! 、cell.setData([indexPath.row]を収集)、そしてsetDataの内部で、この配列をコレクションビューのデータモデルとして設定します – jpulikkottil

答えて

0

これはあなたが探している答えではない場合

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 
} 

それを呼び出す、あなたのViewControllerの配列を追加し、私に教えてください。私は明確にしたいと思っていたので、私はまだコメントする能力がありません。私は、50の評判に達して、コメントしたり、いくつかの説明をすることができるようにする必要があります。

+0

ちょうどsecondHomeViewCellのコードを追加しました。あなたもそれを見て、私にそれを含む答えを与えてください。 –

+0

SecondHomeViewCellで呼び出す方法も教えてください –

0

あなたは今、テーブルビューのデータソース方式

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 
} 
関連する問題