I have a table view cell with a nib, all the outlets are connected properly. I am registering the cell, but the awakeFromNib is not being called. I am well deep into the app and I have registered many cells like this one, but this one is acting funny. The cell is rendered, because it comes inside the if let and when I give it cell.contentView.backgroundColor = .red, it shows the cell on the screen, but without the content like the label or images. What am I missing here?
Just a guess here but maybe you are registering a cell class instead of a nib?
Make sure you call this:
register(UINib(nibName: "MyCell",bundle: Bundle.main), forCellWithReuseIdentifier: "identifier")
and not this:
register(MyCell.self, forCellWithReuseIdentifier: "identifier")
Guessing if you already added "override func awakeFromNib()" function in your Cell class. You will have to override awakeFromNib function in Swift class.
Related
Problem Statement : I have a UIView Nib. Inside that Nib I have UICollectionView. I created Custom UICollectionViewCell and Assigned its class i.e. ServiceCollectionView Cell. I have 2 labels inside that UICollectionViewCell. Now When I try to Make an Outlet for those labels in custom class, I doesn't let me do that. I Have attached an image too. Anyone have an idea where the problem is ? Or What I am doing wrong. Any Help would be much appreciated.
Thanks
In case of adding a collection view/ table view in a UIView with separated .xib file, you should also create a separated .xib custom cell.
In your UIView Custom class, you should implement register(_:forCellWithReuseIdentifier:), for example:
// collectionView is the IBOutlet for the collection view that exists in the view .xib file
collectionView.register(UINib(nibName: "CustomCollectionViewCell", bundle: nil), forCellReuseIdentifier: "CustomCollectionViewCell")
And you will be good to go.
For some reason Xcode opens the interface (yourclass.swift (interface))and not the class. Restart Xcode and switch between automatic and manual did help me.
You create a collection View Cell nib file separately. Now you can give the outlet connection. error is not displayed.
My app reads text files into [Card]. Each Card has a two-letter code at the front, and I break up the cards into different NSTableViews depending on that code. That lets me set the layout in IB so each group of cards has an appropriate display.
I used to have all of the controller code in a single VC, but as the number of tables grew, so did the complexity of this code. Since the views differ primarily in layout and some default settings, they can all descend from a single class. So I did:
class CardView: NSTableView, NSTableViewDataSource, NSTableViewDelegate { ...
and, for one example...
class GeometryView: CardView { ...
Then I went to IB, selected the tableview, and changed it's class to GeometryView. Now I have to set up the delegate and dataSource, and this is where I have my problem : IB will not allow me to drag either setting to either the GeometryView or CardView.
So... do the targets of these IB settings have to be a particular subclass, say NSViewController? Or is there something I'm missing that lets IB see these as targets? I didn't do anything in the original VC, it just worked. Or am I simply doing the wrong thing in IB?
In this image you can see the tableview on the far left, the custom view subclass in the helper, and the connections for the tableview on the right. Any attempt to drag from the connections to anywhere in the helper fails. Note that the two existing connections are to the former delegate VC, which is what I am trying to replace.
I'm not sure why Interface Builder won't let you connect the delegate or data source to itself, but you could do it programatically. The awakeFromNib method is probably the best place for this, as it's called after both initWithFrame and initWithCoder:
override func awakeFromNib() {
delegate = self
dataSource = self
super.awakeFromNib()
}
I need to make an inner tableView and tableViewCell deque with identifier Cell2 just like it's outer tableView & tableViewCell which deque with identifier Cell. The outer is completed with its datasource and delegate, however when I'm trying to connect inner tableView and tableViewCell I don't know where to connect its delegate/datasource to.. do I make a new class that subclass from what? Please help out in SWIFT language or SWIFT2 no OBJECTIVE C Please! Thank you guys!
From your question I understand this you have put your inner table view in main tableview's cell, so you can confirm your inner tableviews data source and delegate in your outer UITableviewcell class. First drag your tableview outlet in UITableviewcell class which you made for your cell and then confirm it in awakefromnib() you can write inside awakefromnib() method like
youroutletnameofinnertableview.datasource = self
youroutletnameofinnertableview.delegate = self
I solved it.
Firstly, you will need to make IBoutlet from innerTableView to your subClass for mine is subCell which inherits from UITAbleViewCell. Than in awakeNIB -> You would put self.innerTableView.delegate = self, self.innerTableView.datasource = self, implement requirement for protocols of uitableview datasource and delegate
I created a custom UICollectionViewCell (let's call it MyCustomCell) using a xib (MyCustomCell.xib) and I want to create a subclass of MyCustomCell (we'll call it MyCustomCell2). The parent of MyCustomCell2 (MyCustomCell) has an IBOutlet called titleLabel. However, titleLabel for MyCustomCell2 always returns nil even after I do
self.collectionView.
registerClass(MyCustomCell2.self, forCellWithReuseIdentifier: cellIdentifier);
in viewDidLoad
and
let cell = collectionView.
dequeueReusableCellWithReuseIdentifier(cellIdentifier, forIndexPath: indexPath) as! MyCustomCell2;
in cellForRowAtIndexPath
Any thoughts on what the issue is?
Firstly, when you use registerClass your outlets are not getting connected. You need to use registerNib.
Secondly, the outlets need to be connected in the xib which you are loading. If you are loading the xib of your subclass you need to connect outlets in it.
In my View Controller I am registering a nib using
[self.tableView registerNib:[UINib nibWithNibName:#"MyTableViewCell" bundle:nil] forCellReuseIdentifier:#"MyCellReuseIdentifier"];
In the cell's nib I have used the same identifier as above.
The problem I have is that although I am able to wire a valueChanged event (for a control in my custom cell) to my View Controller I get a NSInvalidArgumentException, unrecognized selector sent to instance when interacting with the control.
Are you aware of a way to programatically check if the file owner got set after the dequeueReusableCellWithIdentifier call in the tableView:cellForRowAtIndexPath: call?
Cheers, Nick
I don't think you can do it. You are trying to use a xib file to define a cell for use in a storyboard table, and link things to the view controller, and you have the cell in a xib because you use it in several tables, yes?
The only way around this is have found is to create a property on the cell subclass to represent the view controller, and set this to the view controller when the cell is dequeued. It's a bit clumsy and in the end I moved a lot of my code into the cell itself, and just passed the whole model object over to the cell instead. It worked pretty well and felt like a much cleaner design.