UICollectionViewCell not detecting touches - ios

I've tried adding a UIButton to the cell's contentView, which wasn't getting called. Then I tried adding a touchesBegan method to the cell. It's not being called. The cell has it's background color being set, and I can see in the extrapolated view that XCode now provides that it's the topmost view.
Any thoughts as to why interaction might not be working?

I think answer lies into UICollectionView delegate methods and datasource, you don't need touchedBegun etc,
I guess you want to know which cell has been tapped. More codes will definitely helps.
In storyboard do this,
A viewController with UICollectionViewCell inside it.
Add UIViewController subclass in your project and make your storyboards viewController as its subclass.
Add UICollectionView subclass and make your UICollectionViewCell its subclass.
read upon this link http://www.raywenderlich.com/22324/beginning-uicollectionview-in-ios-6-part-12

Related

ios - UITableView didSelectRowAtIndexPath method is not called If I set it's background color to clearcolor

I have created the custom UITableViewCell with UIImageView and UILabel on it.
Now when I tap the UIImageView or UILabel, didSelectRowAtIndexPath method is called. However if I tap outside of UIImageView or UILabel, then didSelectRowAtIndexPath method is not calling.
When I analyse the cause I encounters strange behaviour. In my custom UITableViewCell, I set the background color for UITableViewCell view and it's contentView to clearColor.
If I set background color to UITableViewCell contentView, then the didSelectRowAtIndexPath is called. But if I set the color to clearColor, then the didSelectRowAtIndexPath method is not called.
Why is this happening?
EDIT: IB Design added.
maybe you can export the "cell.subviews" and "cell.contentView.subviews" like this:
NSLog(#"cell.subviews = %#",cell.subviews);
NSLog(#"cell.contentView.subviews = %#",cell.contentView.subviews);
make sure where are your UIImageView and UILabel .
after that ,you can know which view will receive the responds events and resolve it.
By the way ,I hope to see the main code you describe.
hope it helps.
Debug view hierarchy so you can get better idea.
Click the Debug View Hierarchy button () in the bar at the top of the debug area to inspect a 3D rendering of the view hierarchy of your paused app.
By this you will get idea about view hierarchy and came to know which view get response first when you select row
Refer this Apple documentation for more detail.
Hope this will help :)

Can a UICollectionView and UITableView work in a single ViewController

I am trying to implement a UITableView and a UICollectionView (25 cells with buttons) in a single ViewController, is this possible.
What I am looking for, is when I tap on the button inside the cell, it will add the button name to the TableView.
If anyone knows of any examples somewhere or any information. I would be greatful.
You can do it. Just make sure that your ViewController implements both UICollectionViewDataSource and UITableViewDataSource. You will also need a UICollectionViewDelegate in order to detect when a cell is tapped.
On how to implement UICollectionView you can see here: https://www.raywenderlich.com/78550/beginning-ios-collection-views-swift-part-1

How to open new ViewController after click(touch) on ImageView?

I have TableViewController with a custom cells.
For table is responsible class:
#interface CustomTableViewController : UITableViewController
For cells is responsible class:
#interface CustomTableViewCell : UITableViewCell
The ImageView are located in cell of table.
How I can open new controller when I click(touch) at ImageView inside cells?
As it appears I must to use UIViewController near UITableViewCell that to open new viewController, is not?
Usually this kind of stuff is handled inside UITableViewDelegate method didSelectRowAtIndexPath. However the table view calls this method when the entire cell is tapped.
If you want to trigger the action only for the image, you might have to add UITapGesture to the image view. Do not forget that image views have userInteractionEnabled set to false by default. There are two ways to communicate the tap event to the view controller that i can think of right now. Either by delegation or closure. The latter one requires less code.

Where is the place to programmatically adjust the view of a UICollectionViewCell?

I would like to programmatically apply some view modifications once a UICollectionViewCell has been created and all its view outlet are being assigned (right as a UIViewController's viewDidLoad method). However I don't want the code to be re-run when the cell is being reused as this view initialization/modifications have to be done only once. Is there a method that I should override in my custom UICollectionViewCell implementation that is being called as soon as the view outlets are assigned?
There is the applyLayoutAttributes: methods that is just called after the view is added to the collection view. However from its name and its description it doesn't sound to be the right place to initialize the view. In addition this method might be called for every re-use as well. However using an initialized-flag this could be worked around.
If it's being loaded from a storyboard or nib then you can do that in.
- (void)awakeFromNib
If it's in code then there is an init method something like initWithFrame:reuseIdentifier.
Somrthing like that anyway.
These are both run once when the cell is first loaded.

Disabling Auto Scroll UITableView

I want to disable autoscroll to top. I tried both
tableview.scrollsToTop=NO;
[tableview setScrollsToTop:NO];
but that doesn't work.
Thanks
The automatic scrolling code resides in tableViewController, so auto-scrolling can't be disabled. Instead of subclassing from UITableViewController you can subclass from UIViewController and use a tableView inside it.
If you are willing to use UITableViewController itself, you can override viewWillAppear and don't call [super viewWillAppear].

Resources