I have a xib file named MyCell.xib with a custom UITableView cell and MyCell.swift where I have outlets and code that sets constraints.
Now I want to use that xib file in a different UITableView and have MyNewCell.swift inherited from MyCell.swift in order to override the code that set constraints. However, I don't know how to do it because in my xib I can only specify one class as a custom class for my cell. And when I get a cell in cellForRowAtIndexPath I get an object of type MyCell not MyNewCell.
Related
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")ā.
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)
I have a UITableView where I'd like to have multiple types of UITableViewCells. The different types of cells have a lot of similar properties, but have a few key differences, so I was hoping to create a template UITableViewCell class (TemplateCell) that the different cell types could extend.
I've tried doing this by creating a the TemplateCell class and having an associated .xib file. I then tried to go to my UITableView storyboard file and created my various cell types that subclassed my template. However, when I added those cell types to the storyboard file, they showed up as blank and didn't have any of the properties I had in the template file.
Is there an better way to create UITableViewCell templates?
You can create a TemplateCell class and separate classes for every other Cell (inherited from TemplateCell). Then you could assign common IBOutlets of EVERY subclassed cell to it TemplateCell class. Or you could operate with them by looking for the views with specific tags if you don't want to bother with outlets for base class.
Unfortunately you can't draw views in IB respecting some base view, so you should draw each cell separately, but you can use common outlets declared in the base class, and those outlets (properties) that differ, put into inherited cell classes.
Without 3d-parties IB doesn't support loading views from xib to a storyboard or another xib. You can use XXNibBridge for that.
I want to separate the code decide how the cell display from UITableViewCell class for reuse or switch template in runtime. Like this:
-(void)setTemplate:(MyTemplate *)template {
[self.productName setStyleFrom:template.mainLabel];
....
}
And I don't want to use nib files or storyboard, code only.
Thank you.
You can use Prototype Cell if you Don't want to add Cell through .xib or class which is derived from UITableview cell
Click the link for Prototype cell http://www.raywenderlich.com/50308/storyboards-tutorial-in-ios-7-part-1
I have added prototype cell in one of the controllers that is part of the storyboard I'm designing. It has various labels, buttons, bells and whistles.
But how do I know create outlets to that cell's elements? If I click the assistant in Xcode, it will show me the dummy source of my inherited UITableViewController and not of the "DetailsCell" which inherits from UITableViewCell and is specified in the custom class input field in IB.
I could of course use ViewWithTag() but I'd rather have something strong typed.
With any object you can create with IB, you can assign it to a different class (one that you wrote). This is not different for cells. In the same way you can make IB instantiate MyFooView instead of UIView (MyFooView being derived from UIView), you can do the same for the cells.
Click your cell, click the 3rd icon on the shelf to the right. You will see a section called "Custom Class" and a text field called Class. Select the new class you've just created that is derived from UITableViewCell (let's call it MyCell). You can add IBOutlets to the MyCell class.
You first need to create a class DetailedCell and add IBOutlets to that class.
Then in the Storyboard/IB, assign that class to the prototype cell and those outlets will be available to fill.