Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Saturday, February 6, 2016

UICollectionView Select and Deselect issue

UICollectionView Select and Deselect issue


So I have an main object that has many images associated with it. An Image is also an object.

say you have a collection view controller. and in that controller you have

cellForItemAtIndexPath -

well based on the main object, if it has the current image associated with it I want to set selected to true. But I want the user to be able to "un-select" that current cell at any time to remove its association with the main object.

I find that if you set "selected to true" - if there is an relation between the main object and image in cellForItemAtIndexPath, de-selection is no longer an option.

in

didDeselectItemAtIndexPath

and

didSelectItemAtIndexPath

I test with a log to see if they are called. If a cell is set to selected - nether gets called, but If I never set a cell to selected in cellForItemAtIndexPath I can select and deselect all I want.

Is this the intended way a collection view is supposed to work? I read the docs and it does not seem to talk about this being so. I interpret the docs to mean it works the way a table view cell would. with a few obvious changes

This also shows the controller is set up correct and is using the appropriate delegate methods.... hmmmm

Answer by RegularExpression for UICollectionView Select and Deselect issue


I don't know that I understand the problem, but selected status is set per cell and would include all subviews within the cell. You don't explain what you mean by "a main object has many images associated with it." Associated as in subviews? Or what kind of association exactly do you mean?

It sounds like a design problem to me. Perhaps you need a UIView subclass that contains whatever associated objects you need to have; that subclass can then be set as the content view. I do this, for example, where I have an image, a description, and a sound recording related to the image. All are defined in the subclass and then each of these subclasses becomes a content view for a single cell.

I've also used an arrangement to related images to a folder which contains them. Under this set up, folders and images each have a subclass and either one might be attached to a cell as a content view (these are stored in core data as a single entity).

Perhaps you can further explain your problem?

Answer by richarddas for UICollectionView Select and Deselect issue


Have you looked at:

- (BOOL)collectionView:(PSTCollectionView *)collectionView shouldDeselectItemAtIndexPath:(NSIndexPath *)indexPath;  

Please state your problem more clearly and perhaps we can get to the bottom of it. I've just spent some time with UICollectionView.

I think your problem may be stemming from the confusion that if you set cell.selected = YES programmatically, the reason didSelectItemAtIndexPath: is not getting called is because that is only used when the collectionView itself is responsible for the cell's selection (eg. via a tap).

Answer by user2405793 for UICollectionView Select and Deselect issue


I had the same issue, ie. setting cell.selected = YES in [UICollectionView collectionView:cellForItemAtIndexPath:] then not being able to deselect the cell by tapping on it.

Solution for now: I call both [UICollectionViewCell setSelected:] and [UICollectionView selectItemAtIndexPath:animated:scrollPosition:] in [UICollectionView collectionView:cellForItemAtIndexPath:].

Answer by Aneel for UICollectionView Select and Deselect issue


Do you have a custom setSelected method in your Cell class? Are you calling [super setSelected:selected] in that method?

I had a mysterious problem where, I was using multiple selection, I could not deselect cells once they were selected. Calling the super method fixed the problem.

Answer by Fjohn for UICollectionView Select and Deselect issue


I had a Deselect issue with UICollectionView and I found that I was not allowing multiple selection on collectionView. So when I was testing I tried always on the same cell and if single selection is ON you can't Deselect a cell already selected.

I had to add:

myCollectionView.allowsMultipleSelection = YES;  

Answer by Woomin Josh Park for UICollectionView Select and Deselect issue


I don't know why UICollectionView is so messy like this compared to UITableViewController... A few things I found out.

The reason why setSelected: gets called multiple times is because of the sequence methods get called. The sequence is very similar to that of UITextFieldDelegate methods.

The method collectionView:shouldSelectItemAtIndexPath: is called before the collectionView actually selects the cell because it's actually asking "should it be selected"?

collectionView:didSelectItemAtIndexPath: is in fact called after the collectionView selects the cell. Hence the name "did select."

So this is what is happening in your case (and my case, and I had to wrestle hours over this).

A selected cell is touched again by the user to deselect. shouldSelectItemAtIndexPath: is called to check whether the cell should be selected. The collectionView selects the cell and then didSelectItemAtIndexPath is called. Whatever you do at this point is after the the cell's selected property is set to YES. That's why something like cell.selected = !cell.selected won't work.

TL;DR - Have your collectionView deselect the cell in the delegate method collectionView:shouldSelectItemAtIndexPath: by calling deselectItemAtIndexPath:animated: and return NO.

Short example of what I did:

- (BOOL)collectionView:(OPTXListView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {      NSArray *selectedItemIndexPaths = [collectionView indexPathsForSelectedItems];        if ([selectedItemIndexPaths count]) {          NSIndexPath *selectedIndexPath = selectedItemIndexPaths[0];            if ([selectedIndexPath isEqual:indexPath]) {              [collectionView deselectItemAtIndexPath:indexPath animated:YES];                return NO;          } else {              [collectionView selectItemAtIndexPath:indexPath animated:YES scrollPosition:UICollectionViewScrollPositionCenteredHorizontally];                return YES;          }      } else {          [collectionView selectItemAtIndexPath:indexPath animated:YES scrollPosition:UICollectionViewScrollPositionCenteredHorizontally];            return YES;      }  }  

Answer by dava for UICollectionView Select and Deselect issue


This is kind of old but, since I encounter the same issue using swift I will add my answer. When using:

 collectionView.selectItemAtIndexPath(indexPath, animated: true, scrollPosition: .None)  

The cell didn't get selected at all. But when using:

cell.selected = true  

It did get selected but I wasn't able to select/deselect the cell anymore.

My solution (use both methods):

cell.selected = true  collectionView.selectItemAtIndexPath(indexPath, animated: true, scrollPosition: .None)  

When this two methods are called in:

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

It worked perfectly!

Answer by kinora for UICollectionView Select and Deselect issue


Cell selection and deselection is best handled by setting a backgroundView and a selected background view. I recommend making sure that both of these views' frames are set correctly in the layoutSubviews method (if you set the selected and background view via IB).

Don't forget to set your contentView's (if you have one) background color to clear so the correct background view shows through.

Never set the cell's selection directly (i.e. via cell.selected = YES), use the methods designed for this purpose in the collection view. It is clearly explained in the documents, although I will agree the information is somewhat fragmented across guides.

You should not need to poke into a cell's background colors directly in your collectionView datasource.

Also, as a final note, don't forget to call [super prepareForReuse] and [super setSelected:selected] if you are implementing these in your cell's class, as you might be preventing the cell's superclass from doing the cell selection.

Hit me up if you need further clarification on this subject.

Answer by Trent for UICollectionView Select and Deselect issue


Here is my answer for Swift 2.0.

I was able to set the following in viewDidLoad()

collectionView.allowsMultipleSelection = true;  

then I implemented these methods

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {      let cell = collectionView.cellForItemAtIndexPath(indexPath) as! MyCell      cell.toggleSelected()  }    func collectionView(collectionView: UICollectionView, didDeselectItemAtIndexPath indexPath: NSIndexPath) {      let cell = collectionView.cellForItemAtIndexPath(indexPath) as! MyCell      cell.toggleSelected()  }  

finally

class MyCell : UICollectionViewCell {        ....        func toggleSelected ()      {          if (selected){              backgroundColor = UIColor.orangeColor()          }else {              backgroundColor = UIColor.whiteColor()          }      }    }  


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.