Ошибка Sigabrt- UIGestureRecognizer программно

#ios #swift #uicollectionview #uigesturerecognizer #sigabrt

#iOS #swift #uicollectionview #uigesturerecognizer #sigabrt

Вопрос:

В настоящее время я пытаюсь добавить жест прокрутки вверх и вниз к ячейкам в моем 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 обрабатывает wipegesture:]: нераспознанный селектор, отправленный экземпляру 0x7fd2fb61ee20

Комментарии:

1. обновите свой вопрос с помощью одного экрана .

Ответ №1:

Вы должны установить target: self не target: cell, если метод handleSwipeGesture: находится в классе tableViewCell

 var swipeDown = UISwipeGestureRecognizer(target: self, action: "handleSwipeGesture:")
 

Ответ №2:

 var swipeUp = UISwipeGestureRecognizer(target: self, action: #selector(self. handleSwipeGesture))
swipeleft.direction = .Up
cell!.addGestureRecognizer(swipeUp)
 

То же, что и для Down.

Счастливого кодирования.

Комментарии:

1. Все еще получаю siabrt. Я добавил сообщение об ошибке выше

2. @VikramSeshadri вы использовали тот же код, что я вам говорю? пожалуйста, обновите свой вопрос моим