【问题标题】:How to get all collectionview cell index inside tableview cell using Protocol in Swift如何使用Swift中的协议获取tableview单元格内的所有collectionview单元格索引
【发布时间】:2021-09-17 05:13:12
【问题描述】:

我在 tableview 单元格中使用 collectionview。因此,当单击 collectionview 单元格按钮时,会显示我正在使用协议的 viewcontroller..

tableviewcell 和委托的代码:

protocol CustomCellDelegate: class {
func sharePressed(cell: ProposalTableVIewCell)
}

class ProposalTableVIewCell: UITableViewCell, UICollectionViewDelegate,UICollectionViewDataSource {


@IBOutlet weak var attetchmentsCollectionview: UICollectionView!

var delegate: CustomCellDelegate?

public var bidAttatchment: Array<Get_attachments>?


func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    
    return bidAttatchment?.count ?? 0
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "AttatchmentCollectionViewCell", for: indexPath) as! AttatchmentCollectionViewCell
    
    let attatchBid = bidAttatchment?[indexPath.item]
    
    cell.attatchmentLbl.text = attatchBid?.filename
    
    cell.openBtn.tag = indexPath.item
    
    cell.openBtn.addTarget(self, action: #selector(connected(sender:)), for: .touchUpInside)
    
    return cell
 }
@objc func connected(sender: UIButton){

delegate?.sharePressed(cell: self)

}

viewcontroller 的代码:当我按下sharePressed 时,只获取collectionview 的第一个单元格值..如何获取所有单元格值..请告诉我

 class ViewMyAppliedReqVC: UIViewController, UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate, CustomCellDelegate{


func sharePressed(cell: ProposalTableVIewCell) {
    
    guard let index = tableView.indexPath(for: cell)?.row else { return }
    let name = getBitDetails?.result?.bid?.get_attachments?[index].filename// always getting only first cell value
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
    let cell = tableView.dequeueReusableCell(withIdentifier: "ViewProposalTableVIewCell1", for: indexPath) as! ViewProposalTableVIewCell1
    
    cell.bidAttatchment = getBitDetails?.result?.bid?.get_attachments
    cell.delegate = self
    cell.attetchmentsCollectionview.reloadData()
    return cell
}

【问题讨论】:

    标签: swift tableview cell


    【解决方案1】:

    您总是得到相同的索引,因为您正在取出“ProposalTableVIewCell”的索引,这是 tableView 单元格。并且collectionView 单元格在同一个tableView 单元格中。

    解决方案: 在协议函数中取另一个参数,如下所示,用于存储集合单元的索引

        protocol CustomCellDelegate: class {
           func sharePressed(cell: ProposalTableVIewCell,collectionCellIndex:Int)
        }
    
    
        func sharePressed(cell: ProposalTableVIewCell, collectionCellIndex:Int) {
        
           guard let index = tableView.indexPath(for: cell)?.row else { return }
           let name = getBitDetails?.result?.bid?.get_attachments?[collectionCellIndex].filename// This will return index of collection cell
        }
    
    
       @objc func connected(sender: UIButton){
    
          delegate?.sharePressed(cell: self,collectionCellIndex: sender.tag )
    
       }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-04-25
      • 1970-01-01
      • 1970-01-01
      • 2022-01-08
      • 2013-10-30
      • 2018-07-08
      • 1970-01-01
      • 2018-03-13
      相关资源
      最近更新 更多