Swift3 单击复选框时从集合视图单元格获取名称
我在集合视图单元格中有名称和图像。我想获取在特定单元格中单击复选框时选择的名称。我该怎么做 下面是我在收藏视图中的代码Swift3 单击复选框时从集合视图单元格获取名称,swift3,uicollectionview,Swift3,Uicollectionview,我在集合视图单元格中有名称和图像。我想获取在特定单元格中单击复选框时选择的名称。我该怎么做 下面是我在收藏视图中的代码 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath as IndexPath) as! MyCollectionViewCell
cell.imgView.sd_setImage(with: URL(string: str1+self.nameArr [indexPath.row]), placeholderImage: UIImage(named: ""))
cell.lblName.text = self.nameArr [indexPath.row]
cell.lblTime.text = self.timeArr [indexPath.row]
return cell
}
class collectionVC: UIViewController,CellDelegate {
func collectionView(_ collectionView: UICollectionView,
cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath as IndexPath) as! MyCollectionViewCell
cell.imgView.sd_setImage(with: URL(string: str1+self.nameArr [indexPath.row]), placeholderImage: UIImage(named: ""))
cell.lblName.text = self.nameArr [indexPath.row]
cell.lblTime.text = self.timeArr [indexPath.row]
cell.checkButton.tag = indexPath.item
cell.delegateCell = self
cell.indexPath = indexPath
return cell
}
//Mark:- Did tap cell
func didTapCell(index: IndexPath){
let name = self.nameArr[index.item]
print("Your name >> \(name)")
}
}
这是我的手机文件:
class MyCollectionViewCell: UICollectionViewCell {
@IBOutlet weak var checkButton: UIButton!
@IBOutlet var imgView: UIImageView!
@IBOutlet weak var lblName: UILabel!
@IBOutlet weak var lblTime: UILabel!
var tickCheck:Bool = false
@IBAction func btnCheck(_ sender: Any)
{
if tickCheck == false
{
checkButton.setBackgroundImage(UIImage(named: "check"), for: .normal)
tickCheck = true
}
else
{
checkButton.setBackgroundImage(UIImage(named: "uncheck"), for: .normal)
tickCheck = false
}
}
}
您的收藏视图
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath as IndexPath) as! MyCollectionViewCell
cell.imgView.sd_setImage(with: URL(string: str1+self.nameArr [indexPath.row]), placeholderImage: UIImage(named: ""))
cell.lblName.text = self.nameArr [indexPath.row]
cell.lblTime.text = self.timeArr [indexPath.row]
return cell
}
class collectionVC: UIViewController,CellDelegate {
func collectionView(_ collectionView: UICollectionView,
cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath as IndexPath) as! MyCollectionViewCell
cell.imgView.sd_setImage(with: URL(string: str1+self.nameArr [indexPath.row]), placeholderImage: UIImage(named: ""))
cell.lblName.text = self.nameArr [indexPath.row]
cell.lblTime.text = self.timeArr [indexPath.row]
cell.checkButton.tag = indexPath.item
cell.delegateCell = self
cell.indexPath = indexPath
return cell
}
//Mark:- Did tap cell
func didTapCell(index: IndexPath){
let name = self.nameArr[index.item]
print("Your name >> \(name)")
}
}
收集单元
protocol CellDelegate: class {
func didTapCell(index: IndexPath)
}
class MyCollectionViewCell: UICollectionViewCell {
@IBOutlet weak var checkButton: UIButton!
@IBOutlet var imgView: UIImageView!
@IBOutlet weak var lblName: UILabel!
@IBOutlet weak var lblTime: UILabel!
var delegateCell:CellDelegate?
var indexPath:IndexPath?
var tickCheck:Bool = false
@IBAction func btnCheck(_ sender: Any)
{
delegateCell?.didTapCell(index: indexPath!)
if tickCheck == false
{
checkButton.setBackgroundImage(UIImage(named: "check"), for: .normal)
tickCheck = true
}
else
{
checkButton.setBackgroundImage(UIImage(named: "uncheck"), for: .normal)
tickCheck = false
}
}
}
在集合文件中获取错误:delegateCell?.didTapCell(索引:indepath)。错误是:无法将“indexath.type”类型的值转换为预期的参数类型“indexath”。错误已消失,但未调用CollectionView文件中的func didTapCell(索引:indexath)。我在该方法上放置了一个调试器,但该方法未被调用。