How do I create a UICollectionView cell with a specific class? - ios

I am migrating an Objective-C class to Swift. I use the collectionView: cellForItemAtIndexPath: to return a cell of a custom class.
How do I do what I want to do using Swift?

The language change makes no difference. Cocoa works the same way regardless of whether you talk to it with Swift or Objective-C. You register the cell class with collection view (probably in your viewDidLoad) — or the cell nib if the cell is to be loaded from a nib — and in your cellForItem you dequeue it, configure it, and return it.

Related

UICollectionViewCell subclass that uses its parent's xib

I have a custom UICollectionViewCell subclass, lets call it CellClassOne, and I'm trying to create a subclass of that cell, called CellClassTwo so I can change a property and modify some constraints in its view in awakeFromNib.
However, when I register my cell in my collectionView it gets loaded form the xib, so it has the parent's class, CellClassOne. How can I create CellClassTwo that can be dequeued by my collectionView and have its class set to CellClassTwo (and any properties and ovverides with that)?
I'm trying to avoid setting my properties in cellForRow since I'm trying to reuse my cell in different parts of my app but need slightly different paddings for some views, and I don't want to create duplicate Xib files for this.
If I must go with the duplicate files, then it may be better to stick with configuring the views in cellForRow.
I’m not sure that you can achieve what you are looking for via xib, but a viable way that should work is creating the CellClassOne programmatically ( including all the subviews and related layout constraints) following the cell lifecycles, and then subclassing it with CellClassTwo accordingly. That way you should be able to register the cell like ‘ collectionView.register(CellClassTwo.self, forCellReuseIdentifier: "myCell")‘.

Subclassing UITableViewCell and connecting outlets

I'm working in Swift 3 and Xcode 8.
I have a number of table view cell prototypes where the cells are very similar, but there are enough differences where I want to make a subclass of UITableviewCell to hold all the similar outlets and the basic initialization, but then create a few classes which subclass off of the new class.
Question is, how do I get the outlets for these cells all connected to the outlets in the base class?
I can do this for the first cell, but then IB sees those outlets as connected, and won't let me connect them from the next subclassed cell.
I copied the first prototyped cell and pasted it in, then changed it's class to the second type. This still showed the outlets connected, and this seems to work, however, an action for a button in the second cell is not working.
What's the trick to getting all the different subclassed cells to have their outlets and actions connected to the base cell class?
If you have different classes for different cell then take objects of those classes and by using them you will be able to use your different cell's label,button etc
let cell1 = tableView.dequeueReusableCellWithIdentifier("Cell1",forIndexPath: indexPath) as! YourSubClass1
let cell2 = tableView.dequeueReusableCellWithIdentifier("Cell2",forIndexPath: indexPath) as! YourSubClass2
You need to remove the outlets of the second cell and connect them again to the subclass outlet properties after changing the second class type.
If you don't remove the outlets, it will still refer to the outlets of the first cell and that won't work.

How to access UITableViewCell from UITableView in different UIViewController?

I have very complex cell in UITableView within UIViewControllerA. Now I need to use the same cell within UITableView of UIViewControllerB.
How to do this without copy and paste views from one scene to another?
Do not wanna use xib approach.
Is it related to registerNib:forCellReuseIdentifier?
You mentioned you don't want to use xib. Storyboards don't support this and therefore the last option you have is to construct your UITableViewCell in code. With the new Auto Layout anchors, it's not that bad.

Connecting label to UITableViewCell

I've created several tables in the past but I believe it's something with the way I'm doing it that Xcode doesn't like this time.
I have several xib files with a UIView inside. These together creates a slideview like snapchat.
In one of these xib files I have a UIView. In this I have a UITableView and a UITableViewCell. I have set up a prototype cell with an identifier "cell". I've set up the delegate and dataSource as I usually do.
I made a UITableViewCell class and set up the class for the prototype cell.
Here's where the trouble is:
I wanna connect my labels on the cell to the UITableViewCell class and make IBOutlets. But Xcode doesn't let me do it. It simply doesn't connect them.
Any suggestions on why I can't do it or if there's a way around??
Change class of your UITableViewCell to your custom class in xib. (in utilities section,there is option called custom class)

How to reuse custom UITableViewCell defined as a prototyped cell in Storyboard controller

I have the only cell template for items at two different UITableViewControllers/TableViews.
What I need is to define it once and then reuse at other UITableView via
UITableView.DequeueReusableCell(CellId);
The issue is that is when I call this method on UITableView which doesn't contain cell prototype I'm getting NULL.
How to reuse my prototyped cell across multiple table controllers?
I want to define cell template in storyboard, NOT xib.
It turned out that the only way to reuse a cell it to design it with xib and register at tableview that xib with cellid.
Just copy-paste your prototype cell in every table view controller where you need it.
And if I understand your question, in a standard and proper way, it's not possible to dequeue a cell from another table view, Apple implementation handles this mechanism itself.
Using xib for a reusable cell is beneficial while cell design is fixed in the whole app. But when there are conditional requirements or slight changes in design or functionality and remaining design and functionality is same for tableview cell, in this case if you still want to reuse the code then you can subclass tableview cell class.

Resources