【发布时间】:2018-09-25 16:17:20
【问题描述】:
我有自定义类,它们创建一个对象数组,然后为我的 UICollectionView 动态转换为单元格,这些类基本上都如下所示:
import Foundation
class BoardNote : NSObject {
var note_id : String = ""
var itemType : String = ""
var added_by : Any = ""
var link : Any = ""
var content : String = ""
var board_id : Any = ""
var date_added : Any = ""
}
然后使用这个类的一个实例来创建一个像这样的单元格:
let cell = collectionView.dequeueReusableCell(withReuseIdentifier:
"noteViewCell", for: indexPath) as! NoteViewCell
cell.content.text = (itemArray[indexPath.row] as!
BoardNote).content
cell.noteId = (itemArray[indexPath.row] as! BoardNote).note_id
print("made note cell")
return cell
当我尝试在我的集合视图中实现基本拖放时,我收到错误“无法将“BoardNote”类型的值转换为“NSObject”。
我发现我可能还需要将此作为类 NSItemProviderWriting 的扩展,但我不确定如何。
这是我的 DragDelegate 扩展的开始,错误发生在第 4 行,即线程 1:signal SIGABRT:
extension BoardViewController : UICollectionViewDragDelegate
{
func collectionView(_ collectionView: UICollectionView,
itemsForBeginning session: UIDragSession, at indexPath: IndexPath) ->
[UIDragItem]
{
let item = self.itemArray[indexPath.row]
let itemProvider = NSItemProvider(object: item as! NSObject as!
NSItemProviderWriting)
let dragItem = UIDragItem(itemProvider: itemProvider)
dragItem.localObject = item
return [dragItem]
}
谢谢!
【问题讨论】:
标签: ios swift uicollectionview drag-and-drop