Привет, я хочу получить название ячейки внутри UICollectionView
, у меня есть 1 ячейка внутри UICollectionView
ячейка имеет массив категорий, когда я нажимаю UICollectionView
item Я хочу получить название ячейки. Мои коды здесь.Два направленных ячейки внутри коллекции зрения, как получить название ячейки?
Посмотреть контроллер Cell
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
}
}
Нижняя сторона Я пометил с
печати (indexPath.row) // здесь дает коллекции номер вида при нажатии на, но я хочу получить название ячейки, которые внутри категории Пример массива: A/3, B/5
Я хочу видеть там действия, спасибо вам!
привет я сделал, но когда я нажал дает выход как A/5, А/3, А/7, каждая клетка дает, но я нажал C, но дает все из них A – SwiftDeveloper
Я забыл, вам нужно добавить 'prepareforReuse' метод в ячейку, обновленный ответ – iSashok
Я добавил, но то же самое дает вывод все, A/2, как – SwiftDeveloper