Here is my complete code. MenuTableViewCell is called from another controller. It shows up the data and picture. But when tap on a certain section, the tap does not work. After tapping it should go to a different view controller. Everything has to be done programatically.
#MenuTableViewCell.swift
import UIKit
class MenuTableViewCell: UITableViewCell,UICollectionViewDelegate, UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {
var Labeltitle: UILabel?
var colView: UICollectionView?
//屏幕宽度
let SCREEN_WIDTH = UIScreen.main.bounds.width
//屏幕高度
let SCREEN_HEIGHT = UIScreen.main.bounds.height
var arrData = ["旅游英语","商务英语","基础英语","成人英语","少儿英语","青少英语","视频","师资"]
var collectionViewHeight: NSLayoutConstraint?
override func awakeFromNib() {
super.awakeFromNib()
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
}
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
// 设置选中cell时无高亮等效果
self.selectionStyle = .none
let layout = UICollectionViewFlowLayout()
self.colView = UICollectionView(frame: CGRect.init(x: 0, y: 0, width: SCREEN_WIDTH, height: 680), collectionViewLayout: layout);
self.colView?.register(MenuCollectionViewCell.self, forCellWithReuseIdentifier: "colCell")
self.colView?.delegate = self
self.colView?.dataSource = self
self.colView?.backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
self.colView?.showsHorizontalScrollIndicator = true
self.colView?.allowsSelection = true
self.colView?.allowsMultipleSelection = true
//设置间距
//设置水平间距
layout.minimumInteritemSpacing = 0;
//设置垂直间距
layout.minimumLineSpacing = 0;
// layout.itemSize = CGSize(width: (SCREEN_WIDTH/3), height: 125)
layout.itemSize = CGSize(width: (SCREEN_WIDTH/2), height: 170)
self.contentView.addSubview(colView!)
}
//加载数据
func reloadData() {
//collectionView重新加载数据
self.colView?.reloadData()
//更新collectionView的高度约束
let contentSize = self.colView?.collectionViewLayout.collectionViewContentSize
self.collectionViewHeight?.constant = contentSize!.height
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.arrData.count
}
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "colCell", for: indexPath) as! MenuCollectionViewCell
//self.colView?.isUserInteractionEnabled = true
cell.title?.text = arrData[indexPath.row]
cell.isSelected = true
cell.contentView.isUserInteractionEnabled = false;
switch indexPath.row {
case 0:
cell.imgView?.image = UIImage(named:"luyou")
break
case 1:
cell.imgView?.image = UIImage(named:"shangwu")
break
case 2:
cell.imgView?.image = UIImage(named:"jichu-1")
break
case 6:
cell.imgView?.image = UIImage(named:"shiping")
break
case 4:
cell.imgView?.image = UIImage(named:"shaoer")
break
case 5:
cell.imgView?.image = UIImage(named:"qingshao")
break
case 3:
cell.imgView?.image = UIImage(named:"chengren")
break
case 7:
cell.imgView?.image = UIImage(named:"shiziti")
break
default:
cell.imgView?.image = UIImage(named:"mainhead")
break
}
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath){
if(network_status == 0){
self.chrysan.show(.error, message:"没有网络", hideDelay: 1)
return
}
let Publicmenu = PublicmenuViewController()
switch indexPath.row {
case 0:
Publicmenu.menu = "旅游英语1"
Publicmenu.menucateid = 3
Publicmenu.menubanner = UIImage(named:"旅游英语")
break
case 1:
Publicmenu.menu = "商务英语"
Publicmenu.menucateid = 2
Publicmenu.menubanner = UIImage(named:"商务英语")
break;
case 2:
Publicmenu.menu = "基础英语"
Publicmenu.menucateid = 9
Publicmenu.menubanner = UIImage(named:"基础英语")
break
case 3:
let Contentlist = TextbooklistViewController()
Contentlist.menu = "成人英语"
Contentlist.menucateid = 86
// Publicmenu.menubanner = UIImage(named:"成人英语")
first_ViewController()?.navigationController?.pushViewController(Contentlist, animated: true)
return
case 4:
let Contentlist = TextbooklistViewController()
Contentlist.menu = "少儿英语"
Contentlist.menucateid = 84
// Contentlist.menubanner = UIImage(named:"儿童英语")
first_ViewController()?.navigationController?.pushViewController(Contentlist, animated: true)
return
case 5:
let Contentlist = TextbooklistViewController()
Contentlist.menu = "青少英语"
Contentlist.menucateid = 85
// Publicmenu.menubanner = UIImage(named:"青少英语")
first_ViewController()?.navigationController?.pushViewController(Contentlist, animated: true)
return
case 6:
let videolist = PublicvideoViewController()
videolist.menucateid = 39
videolist.menu = "视频"
videolist.menubanner = UIImage(named:"学员动态")
first_ViewController()?.navigationController?.pushViewController(videolist, animated: true)
return
case 7:
let teacher = TeacherViewController()
first_ViewController()?.navigationController?.pushViewController(teacher, animated: true)
return
default:
break
}
first_ViewController()?.navigationController?.pushViewController(Publicmenu, animated: true)
// self.navigationController?.pushViewController(tour, animated: true)
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
Here is the screen shot When I tap anyone of this section, nothing happens. didSelectItemAt does not get called.
so when I click on the certain item of the picture the tap does not work. I have tried a solution self.colView?.isUserInteractionEnabled set to true or false. But it did not work. Collectionview cellForItemAt works. But collectionview didSelectItemAt does not work. I really don't know how to make tap working. Anyone can help me?
After doing view debugging with view debugger, I found there was new layer UITableViewCellContent. This showed up after updating xcode. Instead of doing self.addSubview(colView!), contentView.addSubview(colView!) solved the problem.