【问题标题】:Error when copy from CollectionView cell (Swift)从 CollectionView 单元格复制时出错(Swift)
【发布时间】:2018-04-02 15:00:12
【问题描述】:

我制作拖放应用程序。

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

当我点击集合视图单元格时,出现错误。

我的复制代码:

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] 阅读
来自私人有效用户设置。无法转换类型的值
'DragBook.ImageCollectionViewCell' (0x10e67e0e0) 到
'DragBook.TextCollectionViewCell' (0x10e67e4e0)。 2018-04-02
19:54:48.631190+0500 DragBook[6891:189120] 无法转换
的值 键入“DragBook.ImageCollectionViewCell”(0x10e67e0e0)到
'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")
}

【问题讨论】:

    标签: ios swift drag-and-drop copy uicollectionviewcell


    【解决方案1】:

    问题是单元格可能是 imageCell 并且你强制将其转换为 textCell

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

    【讨论】:

    • 快乐编码:)
    猜你喜欢
    • 1970-01-01
    • 2022-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-04
    相关资源
    最近更新 更多