2015-04-23 117 views
1

我想从我的UICollectionViewCell中删除一个项目(在我的情况下是一个图像)。 会自行重新排列。我已经设法为按钮命中创建一个提醒,但我很困惑如何从我的图像数组中实际删除它。从UICollectionViewController中删除图像

我马西德威控制器 进口的UIKit

let reuseIdentifier = "Cell" 


class PhotosCollectionViewController: UICollectionViewController, PhotosDelegate { 

var photos = Array<Photo>() 

// @IBOutlet weak var collectionOutlet: PhotoCollectionViewCell! 

override func viewDidLoad() { 
    super.viewDidLoad() 


    /*let photo = Photo() 
    photo.url = "http://www.griffith.edu.au/__data/assets/image/0019/632332/gu-header-logo.png" 

    photos.append(photo)*/ 

    // Uncomment the following line to preserve selection between presentations 
    // self.clearsSelectionOnViewWillAppear = false 

    // Register cell classes 
    self.collectionView!.registerClass(PhotoCollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier) 

    // Do any additional setup after loading the view. 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 

/* 
// MARK: - Navigation 

// In a storyboard-based application, you will often want to do a little preparation before navigation 
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
// Get the new view controller using [segue destinationViewController]. 
// Pass the selected object to the new view controller. 
} 
*/ 

// MARK: UICollectionViewDataSource 

override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int { 
    //#warning Incomplete method implementation -- Return the number of sections 
    return 1 
} 


override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
    return photos.count 
} 


/* var valueToPass:String! 

func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) { 
println("You selected cell #\(indexPath.row)!") 

// Get Cell Label 
let indexPath = tableView.indexPathForSelectedRow(); 
let currentCell = tableView.cellForRowAtIndexPath(indexPath!) as UITableViewCell!; 

valueToPass = currentCell.textLabel.text 
performSegueWithIdentifier("yourSegueIdentifer", sender: self) 

}*/ 


override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as PhotoCollectionViewCell 

    let photo = photos[indexPath.row] 
    if let d = photo.data { 
     let image = UIImage(data: d) 
     cell.imageView.image = image 


     photo.url = "\(photo.url)" 
     photo.title = "\(photo.title)" 


    } 

    else{ 
     photo.loadImage { 
      if $0 != nil { 
       collectionView.reloadItemsAtIndexPaths([indexPath]) 
      } 
     } 
    } 

    return cell 
} 

// MARK: UICollectionViewDelegate 


// Uncomment this method to specify if the specified item should be highlighted during tracking 
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
    if let indexPath = sender as? NSIndexPath { 
     let photo = photos[indexPath.row] 
    } 
    if (segue.identifier == "showDetail"){ 
     if let dvc = segue.destinationViewController as? ViewController { 
      if let indexPath = sender as? NSIndexPath { 
       let photo = photos[indexPath.row] 

       //let photo = Photo() 
       dvc.photo = photo 
       // dvc.photosDelegate = self 
      } 
     } 
    } else if (segue.identifier == "addImage"){ 
     if let dvc = segue.destinationViewController as? ViewController { 
      let photo = Photo(url: "", title: "", tags: "") 
      dvc.photo = photo 
      dvc.photosDelegate = self 
      } 
    } 
} 

/* if (segue.identifier == "showDetail") { 

// initialize new view controller and cast it as your view controller 
var viewController = segue.ViewController as 
// your new view controller should have property that will store passed value 
viewController.passedValue = valueToPass 
}*/ 

override func collectionView(collectionView: UICollectionView, shouldHighlightItemAtIndexPath indexPath: NSIndexPath) -> Bool { 
    performSegueWithIdentifier("showDetail", sender: indexPath) 
    return true 
} 





// Uncomment this method to specify if the specified item should be selected 
/*override func collectionView(collectionView: UICollectionView, shouldSelectItemAtIndexPath indexPath: NSIndexPath) -> Bool { 
    performSegueWithIdentifier("deleteAction", sender: indexPath) 
    var cell = collectionView.cellForItemAtIndexPath(indexPath) as UICollectionViewCell? 
return true 
}*/ 


/* 
// Uncomment these methods to specify if an action menu should be displayed for the specified item, and react to actions performed on the item 
override func collectionView(collectionView: UICollectionView, shouldShowMenuForItemAtIndexPath indexPath: NSIndexPath) -> Bool { 
return false 
} 

override func collectionView(collectionView: UICollectionView, canPerformAction action: Selector, forItemAtIndexPath indexPath: NSIndexPath, withSender sender: AnyObject?) -> Bool { 
return false 
} 

override func collectionView(collectionView: UICollectionView, performAction action: Selector, forItemAtIndexPath indexPath: NSIndexPath, withSender sender: AnyObject?) { 

} 
*/ 

func newPhotoAdded(photo : Photo) { 
    photos.append(photo) 
    self.collectionView?.reloadData() 
} 

} 

我的详细视图控制器

import UIKIT 
protocol PhotosDelegate { 

func newPhotoAdded(photo : Photo) 
} 

class ViewController: UIViewController, UITextFieldDelegate { 

var photo: Photo! 
var photosDelegate : PhotosCollectionViewController! = nil 

@IBOutlet weak var textURL: UITextField! 

@IBOutlet weak var textTitle: UITextField! 
@IBOutlet weak var textTags: UITextField! 
@IBOutlet weak var imageView: UIImageView! 
@IBAction func DeleteButton(sender: UIBarButtonItem) { 
    let alert = UIAlertController(title: "Confirm Delete", message: "Do you really want to delete \(textTitle.text)", preferredStyle: .ActionSheet) 
    let deleteAction = UIAlertAction(title: "Delete", style: .Destructive) { println("\($0.title) was pressed")} 
    let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel) { println("\($0.title) was pressed")} 
    alert.addAction(deleteAction) 
    alert.addAction(cancelAction) 
    presentViewController(alert, animated: true, completion: nil) 

} 

override func viewDidLoad() { 
    super.viewDidLoad() 
    self.textTitle.text = photo.title 
    self.textTags.text = photo.tags 
    self.textURL.text = photo.url 
    let urlString = textURL.text 
    loadImageView(urlString) 


    // Do any additional setup after loading the view, typically from a nib. 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 

func textFieldShouldReturn(textField: UITextField) -> Bool { 

    let urlString = textURL.text 

    loadImageView(urlString) 

    textField.resignFirstResponder() 

    return true 

} 

func loadImageView(url : String){ 

    //let p = self.photo 

    self.photo.url = url 
    self.photo.data = nil 
    let DatatoImage: (NSData?) -> Void = { 

     if let d = $0 { 

      let image = UIImage(data: d) 

      self.imageView.image = image 

     }else{ 

      self.imageView.image = nil 



     } 
    } 




    if let d = photo.data { 

     DatatoImage(d) 

    } else { 

     photo.loadImage(DatatoImage) 

    } 

} 


override func viewWillDisappear(animated: Bool) { 
    photo.title = textTitle.text 
    photo.tags = textTags.text 
    photo.url = textURL.text 

    if (photo.url != ""){ 
       if(photosDelegate != nil) { 
     photosDelegate!.newPhotoAdded(photo) 
    } 
    super.viewWillDisappear(animated) 
    } 
} 

} 

如何管理来选择CollectionViewCell特定的细胞,并在特定的细胞中删除的项目。

+0

上的开关按钮,你打开删除警报? 如果是,那么你必须从数组中删除项目在alertview委托方法 didDismissWithButtonIndex:(NSInteger)buttonIndex在该方法中你必须管理你的删除代码重新加载collectionview。 – jogshardik

回答

0
func collectionView(collectionView: UICollectionView, didDeslectItemAtIndexPath indexPath: NSIndexPath) { 
    var cell = collectionView.cellForItemAtIndexPath(indexPath) as UICollectionViewCell? 

} 

使用选定的索引从数组中删除项目

+0

我知道我的细胞已被选中。但是我的功能没有被访问。为什么会发生? – newbee1988

+0

您确认协议吗? – vijeesh

+0

“协议PhotosDelegate”这是我使用的唯一协议。有没有其他额外的协议,我应该使用? – newbee1988