Skip to main content
書式の改善
ソース リンク
cubick
  • 2.1万
  • 5
  • 27
  • 64
import UIKit

class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UIGestureRecognizerDelegate {


class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UIGestureRecognizerDelegate {

    @IBOutlet weak var collectionView: UICollectionView!
 

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    
     let cell : UICollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "coinCell", for: indexPath)
    
    
    cell.backgroundColor = .init(red: CGFloat(drand48()), green: CGFloat(drand48()), blue: CGFloat(drand48()), alpha: 0.6) 

    
     return cell
    
 }

 

    override func viewDidLoad() {
        super.viewDidLoad() 

    
     _ = UINib(nibName: "UICollectionElementKindCell", bundle: nil) 

    
     let layout = UICollectionViewFlowLayout()
        layout.sectionInset = UIEdgeInsets(top: 20, left: 20, bottom: 20, right: 20)
        layout.itemSize = CGSize(width: 100,height: 100)
        collectionView.collectionViewLayout = layout
    
    
    
     let longPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: Selector(("cellLongPressed")))
        longPressGestureRecognizer.delegate = self
        longPressGestureRecognizer.allowableMovement = 15
        longPressGestureRecognizer.minimumPressDuration = 0.6
        collectionView.addGestureRecognizer(longPressGestureRecognizer)
    }
 

    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int)-> Int {
        return 365
    
 }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int)-> UIEdgeInsets {
        return UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
    }
 

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let horizontalSpace : CGFloat = 500
        let cellSize : CGFloat = self.view.bounds.width / 5 - horizontalSpace
        return CGSize(width: cellSize, height: cellSize)
        // gyou saishou yohaku
    } 

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

func cellLongPressed(sender: UILongPressGestureRecognizer){
let point: CGPoint = sender.location(in: collectionView)
let indexPath = collectionView.indexPathForItem(at: point)
if sender.state == UIGestureRecognizer.State.began{


    func cellLongPressed(sender: UILongPressGestureRecognizer){
        let point: CGPoint = sender.location(in: collectionView)
        let indexPath = collectionView.indexPathForItem(at: point)

        if sender.state == UIGestureRecognizer.State.began{
            collectionView.deleteItems(at: [indexPath!])
            print("longPressed")
        }
    } 

}

}

import UIKit

class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UIGestureRecognizerDelegate {

@IBOutlet weak var collectionView: UICollectionView!
 

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    
     let cell : UICollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "coinCell", for: indexPath)
    
    
    cell.backgroundColor = .init(red: CGFloat(drand48()), green: CGFloat(drand48()), blue: CGFloat(drand48()), alpha: 0.6)
    
     return cell
    
 }

 

override func viewDidLoad() {
    super.viewDidLoad()
    
     _ = UINib(nibName: "UICollectionElementKindCell", bundle: nil)
    
     let layout = UICollectionViewFlowLayout()
    layout.sectionInset = UIEdgeInsets(top: 20, left: 20, bottom: 20, right: 20)
    layout.itemSize = CGSize(width: 100,height: 100)
    collectionView.collectionViewLayout = layout
    
    
    
     let longPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: Selector(("cellLongPressed")))
    longPressGestureRecognizer.delegate = self
    longPressGestureRecognizer.allowableMovement = 15
    longPressGestureRecognizer.minimumPressDuration = 0.6
    collectionView.addGestureRecognizer(longPressGestureRecognizer)
}
 

func numberOfSections(in collectionView: UICollectionView) -> Int {
    return 1
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int)-> Int {
    return 365
    
 }

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int)-> UIEdgeInsets {
    return UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
}
 

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    let horizontalSpace : CGFloat = 500
    let cellSize : CGFloat = self.view.bounds.width / 5 - horizontalSpace
    return CGSize(width: cellSize, height: cellSize)
// gyou saishou yohaku
}
override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

func cellLongPressed(sender: UILongPressGestureRecognizer){
let point: CGPoint = sender.location(in: collectionView)
let indexPath = collectionView.indexPathForItem(at: point)
if sender.state == UIGestureRecognizer.State.began{

        collectionView.deleteItems(at: [indexPath!])
        print("longPressed")
        
    }
}

}

import UIKit

class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UIGestureRecognizerDelegate {

    @IBOutlet weak var collectionView: UICollectionView!

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell : UICollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "coinCell", for: indexPath)
        cell.backgroundColor = .init(red: CGFloat(drand48()), green: CGFloat(drand48()), blue: CGFloat(drand48()), alpha: 0.6) 

        return cell
    }

    override func viewDidLoad() {
        super.viewDidLoad() 

        _ = UINib(nibName: "UICollectionElementKindCell", bundle: nil) 

        let layout = UICollectionViewFlowLayout()
        layout.sectionInset = UIEdgeInsets(top: 20, left: 20, bottom: 20, right: 20)
        layout.itemSize = CGSize(width: 100,height: 100)
        collectionView.collectionViewLayout = layout

        let longPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: Selector(("cellLongPressed")))
        longPressGestureRecognizer.delegate = self
        longPressGestureRecognizer.allowableMovement = 15
        longPressGestureRecognizer.minimumPressDuration = 0.6
        collectionView.addGestureRecognizer(longPressGestureRecognizer)
    }

    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int)-> Int {
        return 365
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int)-> UIEdgeInsets {
        return UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let horizontalSpace : CGFloat = 500
        let cellSize : CGFloat = self.view.bounds.width / 5 - horizontalSpace
        return CGSize(width: cellSize, height: cellSize)
        // gyou saishou yohaku
    } 

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    func cellLongPressed(sender: UILongPressGestureRecognizer){
        let point: CGPoint = sender.location(in: collectionView)
        let indexPath = collectionView.indexPathForItem(at: point)

        if sender.state == UIGestureRecognizer.State.began{
            collectionView.deleteItems(at: [indexPath!])
            print("longPressed")
        }
    } 

}
ソース リンク
ricos
  • 11
  • 1
  • 6

terminating with uncaught exception of type NSExceptionの解決方法

collectionViewを使ったアプリを作っています。
セルを長押しすると長押しされたセルが削除される機能を実装したいのですが、実機で長押しをするとデバックエリアにlibc++abi.dylib: terminating with uncaught exception of type NSExceptionと表示されてしまい動きません。
調べてみたところ接続の問題のようだったので、collectionViewのoutlet接続をし直したりしたのですが解決しませんでした。
コードがどこか間違っているのでしょうか・・?
教えていただけたら嬉しいです。何卒よろしくお願いいたします。

import UIKit

class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UIGestureRecognizerDelegate {

@IBOutlet weak var collectionView: UICollectionView!


func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    
    let cell : UICollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "coinCell", for: indexPath)
    
    
    cell.backgroundColor = .init(red: CGFloat(drand48()), green: CGFloat(drand48()), blue: CGFloat(drand48()), alpha: 0.6)
    
    return cell
    
}



override func viewDidLoad() {
    super.viewDidLoad()
    
    _ = UINib(nibName: "UICollectionElementKindCell", bundle: nil)
    
    let layout = UICollectionViewFlowLayout()
    layout.sectionInset = UIEdgeInsets(top: 20, left: 20, bottom: 20, right: 20)
    layout.itemSize = CGSize(width: 100,height: 100)
    collectionView.collectionViewLayout = layout
    
    
    
    let longPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: Selector(("cellLongPressed")))
    longPressGestureRecognizer.delegate = self
    longPressGestureRecognizer.allowableMovement = 15
    longPressGestureRecognizer.minimumPressDuration = 0.6
    collectionView.addGestureRecognizer(longPressGestureRecognizer)
}


func numberOfSections(in collectionView: UICollectionView) -> Int {
    return 1
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int)-> Int {
    return 365
    
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int)-> UIEdgeInsets {
    return UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
}


func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    let horizontalSpace : CGFloat = 500
    let cellSize : CGFloat = self.view.bounds.width / 5 - horizontalSpace
    return CGSize(width: cellSize, height: cellSize)
// gyou saishou yohaku
}
override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

func cellLongPressed(sender: UILongPressGestureRecognizer){
let point: CGPoint = sender.location(in: collectionView)
let indexPath = collectionView.indexPathForItem(at: point)
if sender.state == UIGestureRecognizer.State.began{

        collectionView.deleteItems(at: [indexPath!])
        print("longPressed")
        
    }
}

}