2016-08-04 18 views
1

Hallo Ich möchte Zelle Titel in UICollectionView, ich habe 1 Zelle innerhalb UICollectionView Zelle haben Kategorien Array, wenn ich auf UICollectionView Element Ich möchte Zelle Titel bekommen. Meine Codes hier.Zwei gerichtete Zelle innerhalb der Sammlungsansicht, wie man Zellentitel bekommt?

View-Controller-Zelle

import UIKit 

class ViewController: UIViewController { 
    var categories = ["A", "B", "C", "D", "E"] 
} 

extension ViewController : UITableViewDelegate { } 

extension ViewController : UITableViewDataSource { 

    func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 
     return categories[section] 

    } 

    func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
     return categories.count 
    } 

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return 1 
    } 

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCellWithIdentifier("cell") as! CategoryRow 
     return cell 
    } 

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 

     print(indexPath.row) 

    } 

} 

UICollectionView 


import UIKit 

class CategoryRow : UITableViewCell { 
    @IBOutlet weak var collectionView: UICollectionView! 
} 

extension CategoryRow : UICollectionViewDataSource { 


    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
     return 12 
    } 

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 
     let cell = collectionView.dequeueReusableCellWithReuseIdentifier("videoCell", forIndexPath: indexPath) as! VideoCell 
     return cell 
    } 

} 

extension CategoryRow : UICollectionViewDelegateFlowLayout { 


    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) 

    } 


    //MARK :- UICollectionViewDelegate 
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { 


    print(indexPath.row) // HERE gives collection view number when clicked BUT I WANT TO Get cell title which inside categories ARRAY example: A/3, B/5 



    } 




} 

Unterseite I mit

print (indexPath.row) markiert // HIER gibt Zahl Sammlung Ansicht, wenn darauf geklickt, aber ich möchte Zelle Titel erhalten, die innerhalb Kategorien ARRAY Beispiel: A/3, B/5

Ich möchte Action sehen, danke Ihrer Hilfe!

Antwort

1

Ich glaube, Sie Titel protpery Ihre Klasse

CategoryRow : UITableViewCell { 
    var title: String? 
    @IBOutlet weak var collectionView: UICollectionView! 
} 

hinzufügen müssen, und Sie müssen Methode in CategoryRow

func prepareForReuse() { 
    super.prepareForReuse() 
    title = nil 
} 

in ViewController Satz hinzufügen diesem Titel

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCellWithIdentifier("cell") as! CategoryRow 
     let titleFromCategories = categories[indexPath.row] as? String 
     cell.title = titleFromCategories 
     return cell 
    } 

zur Zelle und in CategoryRow implementieren wie unten

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { 
     let yourString = title + String(format: "/%d", indexPath.row) 

     print(yourString) // HERE gives collection view number when clicked BUT I WANT TO Get cell title which inside categories ARRAY example: A/3, B/5 

} 
+0

hallo ich habe, aber wenn ich gibt Ausgang wie A/5, A/3 geklickt haben, A/7, jede Zelle A gibt, aber ich clicked C, aber gibt alle von ihnen A – SwiftDeveloper

+0

Ich habe vergessen, Sie müssen 'prepareforReuse' Methode in der Zelle hinzufügen, aktualisierte Antwort – iSashok

+0

Ich fügte hinzu, aber die gleiche gibt Ausgabe alle, A/2 wie – SwiftDeveloper

1

Titel aus dem Array wie folgen:

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { 
    let title = String(format: "%@/%d", categories[indexPath.row], indexPath.row) 
    print(title) 
} 
+0

sagt Fehler: Verwendung von ungelösten Kennung 'Kategorien' – SwiftDeveloper

Verwandte Themen