目前我正在尝试向我的UICollectionView中的单元格添加向上和向下滑动手势以执行特定操作。我已经以编程方式完成了这一切,但我得到了sigabrt。有什么建议吗?
class TableViewCell: UITableViewCell, UICollectionViewDataSource, UICollectionViewDelegate {
var collectionView: UICollectionView!
var cellTextLabel: UILabel!
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = UICollectionViewScrollDirection.horizontal
layout.itemSize = CGSize(width: 90, height: 90)
collectionView = UICollectionView(frame: self.bounds, collectionViewLayout: layout)
collectionView.delegate = self
collectionView.dataSource = self
collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "CollectionViewCell")
collectionView.backgroundColor = UIColor(red: 34.0/255.0, green: 40.0/255.0, blue: 51.0/255.0, alpha: 1.0)
collectionView.frame.size.height = 120
collectionView.frame.size.width = 500
self.addSubview(collectionView)
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
}
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 10
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
var arrayOfReviewVocab = ["衣服" + "\n" + "clothes", "学校" + "\n" + "school", "你好" + "\n" + "hello", "书" + "\n" + "books", "5", "6", "7", "8", "9", "10"]
let cell: UICollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionViewCell", for: indexPath) as! UICollectionViewCell
cell.backgroundColor = UIColor(red: 30.0/255.0, green: 35.0/255.0, blue: 46.0/255.0, alpha: 1.0)
cellTextLabel = UILabel(frame: CGRect(x: 20 , y: -20, width: frame.size.width, height: frame.size.height))
cell.contentView.addSubview(cellTextLabel!)
cellTextLabel.textColor = UIColor.white
cellTextLabel.text = arrayOfReviewVocab[indexPath.row]
cellTextLabel.numberOfLines = 0
var swipeUp = UISwipeGestureRecognizer(target: cell, action: "handleSwipeGesture:")
swipeUp.direction = .up
cell.addGestureRecognizer(swipeUp)
var swipeDown = UISwipeGestureRecognizer(target: cell, action: "handleSwipeGesture:")
swipeDown.direction = UISwipeGestureRecognizerDirection.down
cell.addGestureRecognizer(swipeDown)
return cell
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
return CGSize(width: 200, height: 200);
}
func handleSwipeGesture(gesture: UISwipeGestureRecognizer)
{
if(gesture.direction == .up)
{
print("up")
}
if (gesture.direction == .down)
{
print("down")
}
}
}
以下是错误消息:
[UICollectionViewCell handleSwipeGesture:]:无法识别的选择器发送到实例0x7fd2fb61ee20
答案 0 :(得分:0)
如果方法handleSwipeGesture:在TableViewCell类中,你应该设置target:self not target:cell
var swipeDown = UISwipeGestureRecognizer(target: self, action: "handleSwipeGesture:")
答案 1 :(得分:-1)
var swipeUp = UISwipeGestureRecognizer(target: self, action: #selector(self. handleSwipeGesture))
swipeleft.direction = .Up
cell!.addGestureRecognizer(swipeUp)
与Down相同。
快乐编码。