0
我目前正試圖實現UICollectionView與自定義佈局爲了拖拽和重新排序我的UICollectionViewCell。然而,在試圖將其中一個對象項分配給一個新初始化的NSMutabArray時,我的應用程序崩潰了,我能找到的是它提到了Swift Dynamic Cast Failed在其堆棧跟蹤中。當分配NSMutableArray動態轉換失敗
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
想知道什麼應該是正確的分配他們?謝謝
您的數組包含字符串,而不是NSMutableArrays –
@MikePollard謝謝。但如果我要刪除「作爲NSMutableArray」,它給了我一個錯誤,並要求添加與NSMutableArray相同的聲明。我應該如何改變我的代碼行?謝謝 – munfai