我想從我的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特定的細胞,並在特定的細胞中刪除的項目。
上的開關按鈕,你打開刪除警報? 如果是,那麼你必須從數組中刪除項目在alertview委託方法 didDismissWithButtonIndex:(NSInteger)buttonIndex在該方法中你必須管理你的刪除代碼重新加載collectionview。 – jogshardik