从UICollectionViewController中删除图像

时间:2015-04-23 11:36:30

标签: ios xcode uiviewcontroller uicollectionviewcell

我正在尝试从我的UICollectionViewCell中删除一个项目(在我的例子中是一个图像)。     它会自行重新排列吗?我已设法为按钮命中创建警报,但我对如何从我的图像数组中实际删除它感到困惑。

我的MasterView控制器     导入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中选择特定单元格并删除该特定单元格中的项目。

1 个答案:

答案 0 :(得分:0)

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

}

使用所选索引从数组

中删除项目