我目前正在尝试使用自定义布局实现UICollectionView,以便拖动和重新排序我的UICollectionViewCell。但是,在尝试将其中一个对象项分配到新初始化的NSMutabArray时,我的应用程序崩溃了,而我所能找到的就是它在堆栈中提到了 Swift Dynamic Cast Failed 跟踪。
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
array.addObject("a")
array.addObject("b")
array.addObject("c")
array.addObject("d")
array.addObject("e")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func collectionView(collectionView: UICollectionView!, numberOfItemsInSection section: Int) -> Int {
return array.count
}
func numberOfSectionsInCollectionView(collectionView: UICollectionView!) -> Int {
return 1
}
func collectionView(collectionView: UICollectionView!,
layout collectionViewLayout: UICollectionViewLayout!,
sizeForItemAtIndexPath indexPath: NSIndexPath!) -> CGSize{
return CGSizeMake(197, 224);
}
func collectionView(collectionView: UICollectionView!, layout collectionViewLayout: UICollectionViewLayout!, minimumInteritemSpacingForSectionAtIndex section:Int)->CGFloat{
return 0
}
func collectionView(collectionView: UICollectionView!, layout collectionViewLayout: UICollectionViewLayout!, minimumLineSpacingForSectionAtIndex section:Int)->CGFloat{
return 0
}
func collectionView(collectionView: UICollectionView!, cellForItemAtIndexPath indexPath: NSIndexPath!) -> UICollectionViewCell! {
var cell:UICollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("identifier", forIndexPath: indexPath) as UICollectionViewCell
if (cell == nil)
{
cell = collectionView.dequeueReusableCellWithReuseIdentifier("identifier", forIndexPath: indexPath) as UICollectionViewCell
}
var ima:UIImageView
ima = cell.viewWithTag(4) as UIImageView
ima.image = UIImage(named: "anonymous.jpg")
var name:UILabel = cell.viewWithTag(2) as UILabel
name.text = "\(array.objectAtIndex(indexPath.row))"
cell.layer.borderColor = UIColor.lightGrayColor().CGColor
cell.layer.borderWidth = 0.5
return cell
}
func collectionView(collectionView:UICollectionView!, itemAtIndexPath fromIndexPath:NSIndexPath!, willMoveToIndexPath toIndexPath:NSIndexPath)
{
var object:NSMutableArray = NSMutableArray()
//object.addObject(array.objectAtIndex(fromIndexPath.item).mutableCopy())
object = array.objectAtIndex(fromIndexPath.item).mutableCopy() as NSMutableArray
NSLog("\(object)")
array.removeObjectAtIndex(fromIndexPath.item)
array.insertObject(object, atIndex: toIndexPath.item)
}
func collectionView(collectionView:UICollectionView!, itemAtIndexPath fromIndexPath:NSIndexPath!, didMoveToIndexPath toIndexPath:NSIndexPath)
{
}
func collectionView(collectionView:UICollectionView!, itemAtIndexPath fromIndexPath:NSIndexPath!, canMoveToIndexPath toIndexPath:NSIndexPath) ->Bool
{
return true
}
func collectionView(collectionView:UICollectionView!, itemAtIndexPath canMoveItemAtIndexPath:NSIndexPath) -> Bool
{
return true
}
func collectionView(collectionView: UICollectionView!, layout collectionViewLayout: UICollectionViewLayout!, didBeginDraggingItemAtIndexPath indexPath: NSIndexPath!) {
}
func collectionView(collectionView: UICollectionView!, layout collectionViewLayout: UICollectionViewLayout!, didEndDraggingItemAtIndexPath indexPath: NSIndexPath!) {
}
func collectionView(collectionView: UICollectionView!, layout collectionViewLayout: UICollectionViewLayout!, willBeginDraggingItemAtIndexPath indexPath: NSIndexPath!) {
}
func collectionView(collectionView: UICollectionView!, layout collectionViewLayout: UICollectionViewLayout!, willEndDraggingItemAtIndexPath indexPath: NSIndexPath!){
}
}
当我尝试重新排序我的UICollectionViewCell,同时尝试将对象分配给NSMutableArray时,应用程序崩溃了,这是以下行
object = array.objectAtIndex(fromIndexPath.item).mutableCopy() as NSMutableArray
想知道分配它们应该是什么?谢谢
答案 0 :(得分:0)
我以某种方式弄清楚它目前对我有用,不太确定它是否正确或适当。我做的是,
object[0] = array.objectAtIndex(fromIndexPath.item)
由于 Mike Pollard 在评论中提到它们由字符串而不是 NSMutableArray 组成。因此,我将其分配给对象
的第一个索引