Delegate of UICollectionView inside UITableViewCell - ios

I am facing this problem I can't decide who should be delegate of UICollectionView which is subview of UITableViewCell. UITableView has its controller as delegate. But who should be delegate of UICollectionView inside each UITableViewCell? :) And if it is supposed to be controller, how do I recognize correct model that I should use pass the data into UICollectionView since I don't have UITableViewCell instance or NSIndexPath instances available.

Related

How to make a reusable cell for both TableView and CollectionView

I have a TableView and a CollectionView in different ViewController. Now I am showing an exactly same cell in both of them. So I want to create a reusable cell for convenience and easy maintenance.
I tried to create a xib and set the custom class to an UITableViewCell class, then I can register and load it in the UITableView. However, I cannot reuse this xib in the UICollectionView because CollectionView cannot load TableViewCell.
So my question is that is there any good way to make a reusable cell for both TableView and CollectionView?
UITableViewCell <- UIView
UICollectionViewCell <- UICollectionReusableView <- UIView
both are from UIView, so i think you can create a xib for UIView, and use that view in your UITableViewCell and UICollectionViewCell.
Eg: How to load a xib file in a UIView
and since both cell are going to have common code for some cases you can use category, to share the common code Eg: Sharing code between UITableViewCell and UICollectionViewCell

How do I pass data to a button click event in Swift IOS?

In my project, there is a UITableView which contains UICollectionViews in each row. Each UICollectionView contains a UIButton. I implemented those by using a tutorial here:
https://ashfurrow.com/blog/putting-a-uicollectionview-in-a-uitableviewcell-in-swift/
The problem is, I need to add an action to button click. The usual IBAction click works, but I can't pass any data except an integer (tag) to it.
The question is, how do I pass some data to my button click action?
UIButton in UICollectionViewCell
UICollectionViewCell in UICollectionView
UICollectionView in UITableViewCell
UITableViewCell in UITableView
UITableView in UIViewController/UITableViewController
so your button outlet must be on collectionviewcell class and your button's action must be in uicollectionviewcell, and you can pass data from your uicollectionviewcell to UITableViewCell with delegate (uicollectionviewcell-delegate), and UITableViewCell to UIViewController/UITableViewController with another delegate.

How to register cell for a few UITableViews, while every table is in the same UIViewController

I created UITableViewController with some cells. Inside some of those cells, there are another UITableViews with cells with the same layout. How to register cell in way to make it visible for table in another global UITableView:
UITableViewController:
UITableView main
UITableViewCell identifier1
UITableView2
UITableViewCell identifier3
UITableViewCell identifier2
UITableView3
UITableViewCell identifier3
All I need is to prevent create layout of cell in every subtable UITableView main. I would like to register cell in one cell, and use it in every another UITableView in this controller.

What retains a UICollectionViewCell?

I have a custom UICollectionViewCell. After I return it from UICollectionView's datasource, what retains it?
The cell is added to the scroll view (= collection view). Views retain their subviews.
UICollectionView is the special scrollview which was inherited from UIScrollView.Refer this Documentation.
Which will have collection of UICollectionViewCell inside of it.
When you return it from cellForItemAtIndexPath as data source, it will be added (retained) to the UICollectionView by Apple's internal implementation.
So whenever you add some view to other view with addSubView: method , that will be added/retained by parent view.
Any View must have only one parent view. Not more than that.

NSIndexPath for UIButton in UITableViewCell

I'm trying to implement the block method outlined in the answer to the question in this link, but I don't understand how to set the block. Assuming that...
cell.onButtonTapped = ^{
[self buttonSelectedAtIndexPath:indexPath];
}
is pseudo code, how would I actually implement this assuming I have a UITableViewCell (not a custom cell) and a UIButton within that cell. Is it possible to do this without creating my own UITableViewCell subclass?
If you're gonna do it this way, you'd really have to make a UITableViewCell subclass which is a target of its button, has a property for the block and calls the block when the button is tapped.
There's actually a simple way to know from which indexPath a subview (eg. the button here) of a cell is from. First you'll need a point on the tableView's coordinates, which is contained by the subview. Let's say you'll choose the center of the subview. To convert it to the tableView's coordinates:
CGPoint subViewCenter = [self.tableView convertPoint:subview.center fromView:subView.superView]; // because a view's center is on its superview's coordinates
to get the indexPath:
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:subViewCenter];
No, as per the answer you are referring to, you need a custom UITableViewCell. Essentially you are calling a method on the cell and passing the current indexPath for that cell.
Whether you use the block method or the delegate method you will need a custom UITableViewCell as you need somewhere to store the indexPath and a method to set it.

Resources