gpt4 book ai didi

ios - 从 CollectionView 单元格复制时出错(Swift)

转载 作者:搜寻专家 更新时间:2023-11-01 07:03:31 26 4
gpt4 key购买 nike

我制作了拖放应用。

他工作正常,但从单元格复制不工作。

当我点击 Collection View 单元格时,出现错误。

我的复制代码:

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
}
}

错误 - 线程 1: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)

解决方案:

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")
}

最佳答案

问题是单元格可能是 imageCell 而你强行将它转换为 textCell

if let cell = collectionView.cellForItem(at: indexPath) as? TextCollectionViewCell{

}
else
{
/// it's image cell
let cell = collectionView.cellForItem(at: indexPath) as! ImageCollectionViewCell

}

关于ios - 从 CollectionView 单元格复制时出错(Swift),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49613645/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com