【发布时间】:2016-08-04 07:42:55
【问题描述】:
您好,我想在 UICollectionView 中获取单元格标题,当我单击 UICollectionView 项目时,我在 UICollectionView 单元格中有 1 个单元格有类别数组我想获取单元格标题。我的代码在这里。
视图控制器单元格
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
}
}
我标记的底部
print(indexPath.row) // 点击此处给出集合视图编号,但我想获取类别数组中的单元格标题,例如:A/3、B/5
我想在那里看到行动,谢谢你的帮助!
【问题讨论】:
标签: ios swift2 uicollectionview uicollectionviewcell