[英]Index path of a collection view cell that is inside of a table view cell
I have a UICollectionView nested inside of a table view cell. 我有一个嵌套在表视图单元格内的UICollectionView。 How can I get the index path of the horizontal row of the collectionview cell?
如何获取collectionview单元格水平行的索引路径? I tried to use
我尝试使用
let index = cell.tag
print(index as Any)
to print which index was being selected not knowing that it will print a value of zero no matter what cell I select. 要打印选择的索引,无论我选择了哪个单元格,都不知道它将打印零值。 I apologize that I am not experienced with collection views.
抱歉,我对集合视图没有经验。 Any help is much appreciated.
任何帮助深表感谢。 Thanks.
谢谢。
You should design a protocol handling nested UIElement interactions. 您应该设计一个协议来处理嵌套的UIElement交互。 It makes your mind clearer and allocate better addresses
它使您头脑更清晰,分配更好的地址
class ViewwithTable:UIViewController,UITableViewDataSource,collectionCell_delegate{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "temp", for: indexPath) as! NestedViewwithCollection
cell.delegate = self
return cell
}
func didPressed(indexPath: IndexPath) {
//the pressed index!
}
}
class NestedViewwithCollection:UITableViewCell,UICollectionViewDataSource,UICollectionViewDelegate{
var delegate:collectionCell_delegate?
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
return UICollectionViewCell()
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if let del = delegate{
del.didPressed(indexPath: indexPath)
}
}
}
protocol collectionCell_delegate {
func didPressed(indexPath:IndexPath)
}
Use this code for tableView 将此代码用于tableView
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return model.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
return cell
}
override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath)
{
guard let tableViewCell = cell as? TableViewCell else { return }
tableViewCell.setCollectionViewDataSourceDelegate(self, forRow: indexPath.row)
}
Collection View 集合视图
extension ViewController: UICollectionViewDelegate,UICollectionViewDataSource
{
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return model[collectionView.tag].count
}
func collectionView(collectionView: UICollectionView,
cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
{
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell",
forIndexPath: indexPath)
cell.backgroundColor = model[collectionView.tag][indexPath.item]
return cell
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.