你好我想在UICollectionView
裏面得到單元格的標題,我內部有1個單元格UICollectionView
單元格有類別數組當我點擊UICollectionView
項目我想要單元格的標題。我的代碼在這裏。集合視圖中的兩個定向單元格,如何獲取單元格標題?
視圖控制器細胞
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
}
}
底側I標記
打印(indexPath.row)//這裏給出點擊時集合視圖數,但我想獲取其內部類別細胞標題ARRAY示例:A/3,B/5
我想在那裏看到行動,感謝您的幫助!
你好,我做了,但是當我點擊給出像A/5的輸出,A/3,A/7,每一個細胞都給出了,但我點擊C,但給出他們全部A – SwiftDeveloper
我忘了,你需要在單元格中添加'prepareforReuse'方法,更新的答案 – iSashok
我加了但是同樣給出全部輸出,A/2就好像 – SwiftDeveloper