Perform segue with clicked on a button on UICollectionCell - ios

I want to perform a seque from a UICollectionViewCell inside a UICollectionView. There is a UIIMage that is a subview of UICollectionView. There is a button which is a peer view of UICollectionViewCell. I have used the workarround of a transparent button on top of UIIMage to capture touch events on this cell. Now I want to perform a segue when the user clicks this Image on the collection cell. The problem is that I dont know which cell's button triggered the segue and handle it in prepare for segue.
The button triggers an IBAction where the sender is this button. How do I know which cell is its UICollectionCell underneath this transperent button ?

add tags to your buttons and perform different segues depending on the tags. Another way - without buttons would be to add tap gestures to your UIImages and perform segues depending on which image has been tapped ...

Found the solution: Its similar to the one solved for tableview (of course given UICollectionView is a repackaged table view) posted here.
The solution is to use
-(void)buttonPressed:(id)sender {
UITableViewCell *clickedCell = (UITableViewCell *)[[sender superview] superview];
NSIndexPath *clickedButtonPath = [self.tableView indexPathForCell:clickedCell];
...
}

Related

Add multiple buttons to IOS7 Tableview

How would I go about adding several buttons to a tableview to each cell? I need to add a comment and like button to each cell in my tableview and each button will have to be specfic to the row being clicked. How do I go about doing something like this ? Do i place a action inside of
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
or is there a way to programatically set a button to a ibaction from inside cellForRowAtIndexPath that allows me to send parameters?
You can create custom table view cell and add multiple button.
1. Create class that subclasses UITableviewcell with xib
2. In xib delete the view and drag a tableview cell into xib
3. Add multiple buttons in xib
4. Create IBOutlet for each button.
5. In your view controller import your CustomTableViewCell and in CellForRowAtIndexPath method add action for each method and set button tag as indexpath.row
6. Identify the clicked button's indexpath from it's tag
Refer this link
I think this is what you are asking...
[cell.button1 addTarget:self action:#selector(button1Pressed:) forControlEvents:UIControlEventTouchUpInside]; In the
receiver method you can get sender's tag
-(void)button1Pressed:(id)sender{
UIButton *button1 = (UIButton*)sender;
int selectedRow = sender.tag;
}
You need to subclass your UITableViewCell so you can customise it to your need.
There are a lot of tutorials about this. Depends if you are using Storyboard or not. Here is one of many: http://zeroheroblog.com/ios/how-to-create-simple-tableview-with-custom-cells

Redirecting tap behavior to another layer in a UICollectionViewCell

I am trying to figure out how to change the view in a UICollectionViewCell that triggers the UICollectionView's didSelectCellAtIndexPath. I want to have a top layer that can be tapped to activate, or panned to reveal a lower layer of buttons.
I can add a view on top, and achieve the pan to reveal gesture, but I have to tap the underlying view to trigger didSelect.
Also, I am not using xib's or storyboards, this is all in code.
Any Ideas?
Thanks to #peko, I figured it out.
- (void)selectCell {
UICollectionView *collectionView = (UICollectionView *)self.superview;
NSIndexPath *indexPath = [collectionView indexPathForCell:self];
[collectionView.delegate collectionView:collectionView didSelectItemAtIndexPath:indexPath];
}
This allows me to simulate selection of the cell from within the view controller.

Make UITableViewCell behave like a real button

I want to make UITableViewCell to behave like real button.
Until know I have been using the
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath`
trick, but this is not optimal because it doesn't behave like a button on tap/drag/release.
If you tap a cell row and drag your finger over the cell, it will not get selected when you release your finger (but a button would launch its action in the same case).
Is there any simple way of making a UITableViewCell to behave like a real button without resorting to insert an actual UIButton inside the cell?
You can just create table view cells with a button in them, set the buttons tag to the row so you can workout which row the button belongs to when you receive the button event. Just make sure you reset the buttons tag when you return a reused table view cell instead of creating a new one.
Subclass the UITableViewCell and use the following method:
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
[super setHighlighted:highlighted animated:animated];
if (highlighted) {
_backgroundImageView.image = [UIImage imageNamed:#"img_h"];
}
else {
_backgroundImageView.image = [UIImage imageNamed:#"img"];
}
}
Where img is a plain image and img_h is a highlighted version of that image.
One way is to create a UIButtton of size of your cell and added it to the cell.
Or else you could simply add a UITapGestureRecognizer to your UITableViewCell and that will do the work for you.

How to hide the particulars uitableViewCell webview in when i click on that particuar cells button uitableview?

In tableviewcells i have a buttons and webViews, with some array count.so when i tap on particular tableViewcells button action. i have to hide that particular tables cells webview?
You can do it like this, In UIButton action, you will get cell index if you've given tag to your button, using that tag, you can make cell object with NSIndexPath for the cell you've tapped, hide UIWebView subviews within that cell, you may need to set its height base on some condition which you've made in heightForRowAtIndexPath of UITableView. Another way to get the cell is, [button superview]; here you'll get containerview for particular cell, and apply the same logic, this would only work if you've added button and webview in contentview.
[cell.webView setUserInteractionEnabled:NO];
Make the webView can't get the user touch focus, so that the cell will get the user interaction : )

Changing the contentView when an Accessory button is tapped

I've subclassed UITableViewCell to display a UIImage and two UILabel subviews. In the view controller for the table view, in the method cellForRowAtIndexPath: I've enabled an accessory view via setAccessoryType:UITableViewCellAccessoryDetailDisclosureButton.
Cells display correctly.
When I tap on the accessory disclosure button I want to replace the two label subviews with two different label subviews. My approach was the following:
In the subclassed UITableViewCell, inside layoutSubviews, create the
CGRects for the "alternate" labels, position them in the same
places as the two "original" label and hide them via setAlpha:;
When the disclosure button is tapped swap out the set of two
label by adjusting their respective alphas.
The problem is I can't figure out what logic in layoutSubviews I'd use to know whether the accessory button has been tapped. I see that in the view controller accessoryButtonTappedForRowWithIndexPath: is called when that button is tapped and from there it seems like I would call layoutSubviews but can't figure out how to use that fact to accomplish what I'm trying to do.
Am I going about this all wrong? Instead of hiding/showing CGRects with alpha should I simply be creating another subclass of UITableViewCell?
When I tap on the accessory disclosure button I want to replace the two UILabel subviews with two different UILabel subviews.
I'll do the following. Create a public method in your UITableViewCell subclass like the following:
- (void)changeContentForCell;
In this method you could set the contentView as you prefer.
Then in your view controller implement
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
CustomCell* cCell = (CustomCell*)[tableView cellForRowAtIndexPath:indexPath];
[cCell changeContentForCell];
}
This is a simple example for change the content. In my opinion you don't have to use layoutSubviews to add views.
Leave this logic in changeContentForCell and then call setNeedsLayout to change your layout. Maybe you could have a variable that tracks the state for your cell: normal state or modified state.
Hope it helps.

Resources