Tap 在 didSelectItemAt 中不起作用,它不响应任何内容。谁能帮我看看这个问题?
Tap does not work inside didSelectItemAt, it does not response to anything. Can anyone help me to look at this issue?
这是我的完整代码。 MenuTableViewCell 是从另一个控制器调用的。它显示了数据和图片。但是当点击某个部分时,点击不起作用。点击后它应该转到不同的视图控制器。一切都必须以编程方式完成。
#MenuTableViewCell.swift
导入 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
}
}
这是屏幕截图
When I tap anyone of this section, nothing happens. didSelectItemAt does not get called.
所以当我点击图片的某个项目时,水龙头不起作用。我已尝试将解决方案 self.colView?.isUserInteractionEnabled 设置为 true 或 false。但它没有用。 Collectionview cellForItemAt 有效。但是 collectionview didSelectItemAt 不起作用。我真的不知道如何让水龙头工作。谁能帮帮我?
用视图调试器进行视图调试后,我发现有新层UITableViewCellContent。这在更新 xcode 后出现。 self.addSubview(colView!) 而不是 contentView.addSubview(colView!) 解决了问题。
这是我的完整代码。 MenuTableViewCell 是从另一个控制器调用的。它显示了数据和图片。但是当点击某个部分时,点击不起作用。点击后它应该转到不同的视图控制器。一切都必须以编程方式完成。
#MenuTableViewCell.swift
导入 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
}
}
这是屏幕截图 When I tap anyone of this section, nothing happens. didSelectItemAt does not get called.
所以当我点击图片的某个项目时,水龙头不起作用。我已尝试将解决方案 self.colView?.isUserInteractionEnabled 设置为 true 或 false。但它没有用。 Collectionview cellForItemAt 有效。但是 collectionview didSelectItemAt 不起作用。我真的不知道如何让水龙头工作。谁能帮帮我?
用视图调试器进行视图调试后,我发现有新层UITableViewCellContent。这在更新 xcode 后出现。 self.addSubview(colView!) 而不是 contentView.addSubview(colView!) 解决了问题。