2016-09-10 58 views
0

我目前正在構建一個應用程序,其目標是用戶可以通過單擊單元格內的按鈕將圖像上載到所需的集合視圖單元格。到目前爲止,我已經能夠選擇一個圖像,但它永遠不會去指定的單元格。爲了簡化問題,我將展示我用於前三個單元的代碼。使用圖像選取器將圖像加載到兩個集合視圖單元格

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 
let returnCell = collectionView.dequeueReusableCellWithReuseIdentifier("cell1", forIndexPath: indexPath) as! FirstCollectionViewCell 

    if indexPath.row == 0 { 
     let cell1 = collectionView.dequeueReusableCellWithReuseIdentifier("cell1", forIndexPath: indexPath) as! FirstCollectionViewCell 
     cell1.backgroundColor = UIColor.redColor() 
     cell1.firstAdd.addTarget(self, action: "importPicture:", forControlEvents: UIControlEvents.TouchUpInside) 
     currentPickerTarget = cell1.firstImages 
     return cell1 

    } else if indexPath.row == 1 { 

     let cell2 = collectionView.dequeueReusableCellWithReuseIdentifier("cell2", forIndexPath: indexPath) as! SecondCollectionViewCell 
     cell2.backgroundColor = UIColor.blueColor() 
     cell2.secondAdd.addTarget(self, action: "secondImport:", forControlEvents: UIControlEvents.TouchUpInside) 
     secondPickerTarget = cell2.secondImages 
     return cell2 

    } else if indexPath.row == 2 { 
     let cell3 = collectionView.dequeueReusableCellWithReuseIdentifier("cell3", forIndexPath: indexPath) as! ThirdCollectionViewCell 
     cell3.backgroundColor = UIColor.redColor() 
     cell3.thirdAdd.addTarget(self, action: #selector(ViewController.importPicture(_:)), forControlEvents: .TouchUpInside) 
     return cell3 
    } 
return returnCell 

} 

func importPicture(sender: UIButton) { 

    let point = myCollectionView.convertPoint(CGPoint.zero, fromView: sender) 
    let indexPath = myCollectionView.indexPathForItemAtPoint(point) 
    let cell1 = myCollectionView.dequeueReusableCellWithReuseIdentifier("cell1", forIndexPath: indexPath!) as! FirstCollectionViewCell 

    currentPickerTarget = cell1.firstImages 

    imagePicker.delegate = self 
    imagePicker.sourceType = .PhotoLibrary 
    imagePicker.allowsEditing = true 

    presentViewController(imagePicker, animated: true, completion: nil) 
} 

func secondImport(sender: UIButton) { 
    let point = myCollectionView.convertPoint(CGPoint.zero, fromView: sender) 
    let indexPath = myCollectionView.indexPathForItemAtPoint(point) 
    let cell2 = myCollectionView.dequeueReusableCellWithReuseIdentifier("cell2", forIndexPath: indexPath!) as! SecondCollectionViewCell 

    print("\(indexPath!.row)") 

    secondPickerTarget = cell2.secondImages 
    secondPicker.delegate = self 
    secondPicker.sourceType = .PhotoLibrary 
    secondPicker.allowsEditing = true 

    presentViewController(secondPicker, animated: true, completion: nil) 

} 

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) { 

    imagePicker.dismissViewControllerAnimated(true, completion: nil) 
    secondPicker.dismissViewControllerAnimated(true, completion: nil) 

    let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage 
    currentPickerTarget.image = pickedImage 
    secondPickerTarget.image = pickedImage 
} 

回答

0

您可以默認圖像或空白圖像陣列,並添加上述imageview的每一個細胞按鈕。根據單元格將標籤分配給按鈕。 當用戶點擊任何單元格檢測到它的計數,然後從數組中的索引庫或相機更新對象中選擇一張圖片並重新加載collectionview,或者您可以重新加載集合視圖中的特定單元格。 希望這有助於:)

相關問題