I make Drag and Drop app.
He is work fine, but copy from cell not work.
When i tap on collection view cell, i have error.
My code for copy:
let cell = collectionView.cellForItem(at: indexPath) as! TextCollectionViewCell
UIPasteboard.general.string = cell.textLabel?.text
cellForItemAt
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let item = everitems[indexPath.row]
switch item {
case .text(let content):
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "TextCollectionViewCell", for: indexPath) as! TextCollectionViewCell
cell.configureWithText(content)
cell.layer.cornerRadius = 8.0
cell.layer.masksToBounds = true
return cell
case .image(let content):
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ImageCollectionViewCell", for: indexPath) as! ImageCollectionViewCell
cell.layer.cornerRadius = 8.0
cell.layer.masksToBounds = true
cell.configureWithImage(content)
return cell
}
}
Error - Thread 1: signal SIGABRT
2018-04-02 19:54:35.520913+0500 DragBook[6891:189120] [MC] Reading
from private effective user settings. Could not cast value of type
'DragBook.ImageCollectionViewCell' (0x10e67e0e0) to
'DragBook.TextCollectionViewCell' (0x10e67e4e0). 2018-04-02
19:54:48.631190+0500 DragBook[6891:189120] Could not cast value of
type 'DragBook.ImageCollectionViewCell' (0x10e67e0e0) to
'DragBook.TextCollectionViewCell' (0x10e67e4e0). (lldb)
Solution:
if let cell = collectionView.cellForItem(at: indexPath) as? ImageCollectionViewCell{
UIPasteboard.general.image = cell.imageView?.image
print("Image")
} else if let cell = collectionView.cellForItem(at: indexPath) as? TextCollectionViewCell{
UIPasteboard.general.string = cell.textLabel?.text
print("Text")
}
The problem is that the cell may be imageCell and you force cast it as textCell
if let cell = collectionView.cellForItem(at: indexPath) as? TextCollectionViewCell{
}
else
{
/// it's image cell
let cell = collectionView.cellForItem(at: indexPath) as! ImageCollectionViewCell
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.