我在我的TableViewCell
类中创建了一个自定义委托,并在tableView类中调用它,但不知道为什么会出现这个错误。
TableView类: -
//Cell For Row at indexPath
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
if (0 == indexPath.section)
{
let cell = tableView.dequeueReusableCell(withIdentifier: "FirstRowCell") as! FirstRowCell
cell.btnReview.addTarget(self, action: #selector(GotoReview), for: UIControlEvents.touchUpInside)
//cell.
return cell
}
else if ( 1 == indexPath.section)
{
let identifier = "TableCollectionCell"
var tableCollectionCell = tableView.dequeueReusableCell(withIdentifier: identifier)
if(tableCollectionCell == nil)
{
let nib:Array = Bundle.main.loadNibNamed("TableCollectionCell", owner: self, options: nil)!
tableCollectionCell = nib[0] as! TableCollectionCell
//Delegate
tableCollectionCell.delegate = self
}
return tableCollectionCell!
}
else
{
let identifier = "BrandImagesTableCell"
var brandImagesTableCell = tableView.dequeueReusableCell(withIdentifier: identifier)
if(brandImagesTableCell == nil)
{
let nib:Array = Bundle.main.loadNibNamed("BrandImagesTableCell", owner: self, options: nil)!
brandImagesTableCell = nib[0] as? BrandImagesTableCell
}
return brandImagesTableCell!
}
}
extension HomeViewController : CollectionViewCellTap
{
func didTapCollectionViewCell(indexPath: Int) {
let gotoCreateGroup = self.storyboard?.instantiateViewController(withIdentifier: "CreateGroupView") as! CreateGroupView
self.present(gotoCreateGroup, animated: true, completion: nil)
}
}
从UITableViewCell类调用: -
protocol CollectionViewCellTap
{
func didTapCollectionViewCell(indexPath : Int)
}
class TableCollectionCell: UITableViewCell {
@IBOutlet weak var collectionView: UICollectionView!
var delegate : CollectionViewCellTap?
override func awakeFromNib() {
super.awakeFromNib()
//collectionView cell
collectionView!.register(UINib(nibName: "CreateGroupCell", bundle: nil), forCellWithReuseIdentifier: "CreateGroupCell")
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
}
//Collection View
extension TableCollectionCell : UICollectionViewDataSource ,UICollectionViewDelegateFlowLayout
{
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
{
return 4
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CreateGroupCell", for: indexPath) as! CreateGroupCell
cell.groupName.text = ""
CreateCardView(viewCorner: cell.cardView)
if(0 == indexPath.row)
{
//cell.btnDotted.tag = indexPath.row
//cell.btnShare.tag = indexPath.row
cell.btnDotted.setImage(UIImage(named: "Info"), for: UIControlState.normal)
cell.btnShare.setImage(UIImage(named : "AddGroup"), for: UIControlState.normal)
}
else if(indexPath.row >= 1 && indexPath.row <= 3)
{
cell.btnDotted.isHidden = true
cell.btnShare.isHidden = true
cell.groupImage.image = UIImage(named: "group_dull_card.png")
}
else
{
}
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if ( 0 == indexPath.row) {
delegate?.didTapCollectionViewCell(indexPath: indexPath.row)
}
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: 200, height: 150)
}
}
我是iOS新手。
当你使用CustomTableCell
启动单元格对象时,你需要明确指定dequeueReusableCell(withIdentifier:)
的类型,否则它认为UITableViewCell
的对象没有属性delegate
,所以只需在末尾添加as! TableCollectionCell
,所以现在你可以访问属性类TableCollectionCell
。
var tableCollectionCell = tableView.dequeueReusableCell(withIdentifier: identifier) as? TableCollectionCell