Can a UICollectionView and UITableView work in a single ViewController - ios

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

Related

UICollectionViewCell not detecting touches

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

Can a property of a UITableView be the UITableView Delegate and DataSource

I have a custom view that's a bit of a hack. Basically it's a UIView with tableView as it's property, with additional views in the tableView that need their own delegates. I need a viewController for the UITableView and can't make the UIView it's delegate according to this SO link Custom UIView as UITableView delegate and datasource?.
Can I make the UITableView have a property of UIViewController and set that UIViewController as the tableView's delegate?
In this case according to OOP, the UITableView has a UIViewController so technically, I could expect this to work. But, I am wondering if down the line somewhere this could create problems since the UITableView and UIViewController are coupled in this way.
You don't need a UIViewController for the UITableView - you just need an object or objects that implement the data source and a delegate protocols. As per the accepted answer on the question you linked to you can use a separate controller class to provide this.
The right answer depends a little on how the table is used with your UIView subclass.
If the table will always have the same content (Say a list of months) and there is no value in exposing or abstracting the properties then you can code the delegate and dataSource inside your UIView subclass or in an assistant class.
If the table content will vary depending on how the UIView is used (say a list of people where you don't know what the list is - friends, relatives, employees...) then it would make sense to simply expose the tableview's datasource (and delegate if necessary) properties via your UIView subclass

UISearchBar acting like a UITableView Cell

I have a UISearchBar implemented into my UITableViewController, my problem is when I scroll down in my table view, the search bar acts like a cell and scrolls with the table, therefore disappearing. Is there a war to programmatically fix this?
I know there are similar questions on SO with this issue and I have exhausted all the resources provided in those posts.
Thanks
Sure, don't use an UITableViewController but use a normal UIViewController with UITableView and UISearchBar inside.
When you use an UITableViewController, its UIView is directly the UITableView. So every control and subview go in the tableView. For this reason you need to change strategy and use the way above.

How to prevent UITextView from moving up on keyboard show

I have a UITableViewController and UITextView in one of the cells.
When a user taps on UITextView all the table moves up tableView.frame.origin.y = -88.
As far as I can tell UITableViewController forces the table to move up. Is there a way to prevent this kind of behaviour?
You cannot disable autoscrolling on the UITableViewController.
The best thing to do would probably be to use a UIViewController as a table view's delegate and datasource. Check this answer out.
iOS: Disable UITableView animation when keyboard shows up
Just use a UIViewController instead of a UITableViewController - and make sure it conforms to UITableViewDelegate and UITableViewDatasource protocols.

Insert UITableViewController into Other UIView

I've got a UITableViewController that inserts custom UITableViewCells into a UITableView. Is there a way that I can put this UITableController/View combo into different views? I've got the behavior of the UITableView acting the way I want, I just want to use that UITableView in different UIViews (say a UIView with a UILabel above the UITableView and one with a UILabel below the UITableView).
I know I can make a UIViewController that has an embedded UITableView in it and have the UIViewController act as the UITableView's delegate, but then I would have code reuse (the UITableViewController logic would be in multiple UIViewControllers). Or am I looking at this problem the wrong way?
I want to somehow reuse the functionality of my UITableView in different UIViews.
Thanks
Yes, you can, simply instantiate (or get a reference to) the UITableViewController inside the UIViewController and call something like this:
[self.view addSubview:tableViewController.tableView];

Resources