JVS
JVS

Reputation: 2682

Edit Difficulties in UICollectionViewCell with Button

I've implemented an Edit button, which adds a button to my UICollectionViewCell. When I press Edit => Edit turns into "Done" and when i press Done it is supposed to delete the Button. My Code does allow me to add the buttons, but doesn't hide them. Any Ideas?

 @IBAction func editButton(sender: AnyObject) {

if(editButton.titleLabel?.text as String! == "Edit")
  {

    editButton.setTitle("Done", forState: UIControlState.Normal)
    for item in self.mainCollectionView.visibleCells() as! [SettingsCollectionViewCell] {

        let indexpath : NSIndexPath = self.mainCollectionView!.indexPathForCell(item as SettingsCollectionViewCell)!
        let cell : SettingsCollectionViewCell = mainCollectionView!.cellForItemAtIndexPath(indexpath) as! SettingsCollectionViewCell

        //Close Button
        let close : UIButton = cell.collectionViewButton
        close.hidden = false
    }
} 
else 
{
    editButton.setTitle("Edit", forState: UIControlState.Normal)
    self.mainCollectionView?.reloadData()
 }}

And cellForRowAtIndexPath:

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell 
     {

    let collectionCell:SettingsCollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("collectionCell", forIndexPath: indexPath) as! SettingsCollectionViewCell

      if self.editButton.titleLabel?.text as String! == "Edit" 
        {
          collectionCell.collectionViewButton.hidden = true
        }
     else if editButton.titleLabel?.text as String! == "Done"
        {
         collectionCell.collectionViewButton.hidden = false
        }

        collectionCell.collectionViewButton!.layer.setValue(indexPath.row, forKey: "index")
        collectionCell.collectionViewButton!.addTarget(self, action: "deletePhoto:", forControlEvents: UIControlEvents.TouchUpInside)

        return collectionCell
}

Upvotes: 0

Views: 86

Answers (2)

owlswipe
owlswipe

Reputation: 19469

I think the best strategy here would be to create a button on the main storyboard and check 'hidden' on the button's context menu.

To change a button's text: create an outlet (yes, an outlet) for the button. @IBOutlet var editbutton: UIButton!

Then to unhide it:

editbutton.hidden = false

(you can put this inside an if statement if desired)

Inside an action of the button (you must assign the button to an action as well) do this to hide the button if it has already been pressed into "Done":

if editbutton.title == "Done" && editbutton.hidden == false {
editbutton.hidden = true
}

Then, to change the text of the button when it still says edit:

if editbutton.title == "Edit" {
editbutton.title = "Done" 
}

The actions must be done in this order, or else the button will turn into "Done" and not be hidden, so the computer will go on and hide it before the user presses it a second time.

Upvotes: 1

deoKasuhal
deoKasuhal

Reputation: 2897

You need to change the logic in cellForItemAtIndexPath for retrieving the title from the button.

In order to retrieve title text from the button you need to use titleForState(UIControlState) instead of titleLabel?.text.

Here is code snippet for conditional statement in cellForItemAtIndexPath

if self.editButton.titleForState(.Normal) == "Edit" {
       collectionCell.collectionViewButton.hidden = true
}
else if self.editButton.titleForState(.Normal) == "Done" {
    collectionCell.collectionViewButton.hidden = false
}

Upvotes: 1

Related Questions