Problem connecting outlets when UICollectionViewCell is in a xib - swift

I have a custom UICollectionViewCell defined in a .xib that is connected to a custom class file. I am able to register and display this empty cell in a collection view. I'm even able to set values for the cell like background color in the awakeFromNib function in the custom class. As soon as I add a label (myLabel) to the cell in the xib and make an outlet for it in its custom class file, I get a "this class is not key value coding-compliant for the key myLabel" crash when I run. What is causing this?
I searched through the suggested duplicate question and wasn't able to find a cause there. I think the problem may have more to do with my xib containing a UICollectionViewCell instead of a UIView. Perhaps I'm missing something that requires special attention when setting this up.

I ended up fixing this by deleting the swift file and xib file for the UICollectionViewCell and starting over. Not necessarily an answer to the problem but easier than wasting time trying to fix a file that was small to begin with. On the second try though when I made the class file I checked the box to automatically create the xib. This is better than making the two files separately as Xcode will put the collection view cell in the xib for you.

Related

UITextField inside of custom UITableView is nil in Swift

I have a UITableView in my current project. Each cell is an object of custom class DecisionItem, which implements UITableViewCell. Inside each of these DecisionItems should be a UITextField named "descriptionBox". However, each time I run this app, descriptionBox keeps showing up as nil. Why is this?
This is what shows up in the console, as you can see, descriptionBox is nil:
This is my prototype cell:
My cells show up entirely blank while running the app:
These are things that I have made sure I've done:
Yes, DecisionItem has an IBOutlet variable named descriptionBox that is connected to the UITextField in the prototype cell.
Yes, I set the prototype cell's class to the custom class DecisionItem
Yes, I cast the cell to DecisionItem in the cellForRowAt function
Yes, my reuse identifiers are all correct.
What could be causing this error?
In your storyboard on the left side scene hierarchy right click on the DecisionItem cell and hover over descriptionBox to make sure it highlights the correct text field in the storyboard. If you see a yellow warning sign click the X and relink it.

why do i get "Must translate autoresizing mask into constraints to have _setHostsLayoutEngine:YES" in xcode 6 beta

I have the following code in a swift UITableViewController, but i get a "Must translate autoresizing mask into constraints to have _setHostsLayoutEngine:YES exception at the 2nd line. I didnt change any settings in the interface builder (so Autolayout and size classes are both checked).
override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell? {
let cell:TodoItemCell = tableView.dequeueReusableCellWithIdentifier("TodoItemCell", forIndexPath: indexPath) as TodoItemCell
let row = indexPath.row
cell.titleLabel.text = self.todoItems![row].title
self.callback!(row)
// Configure the cell...
return cell
}
What am i doing wrong?
Not sure how much this helps, but I see the same error if I try to place a cell directly in a UIView rather than in a table. This worked fine with Xcode 5 so I suspect it is a bug with XCode6/ios8. In the mean time, debug into that method and look at your tableView. Make sure it actually has an instance of the cell you are trying to retrieve.
Update: Filed a bug report with Apple and I can confirm that this is an ios8 issue. Unfortunately it is still there as of beta-5, hopefully they will have it fixed before the final release.
This is NOT a bug, you simply cannot use a UIView as cell for table view, you must make sure the top level view in the nib file is a UITableViewCell.
I had this issue because I changed the Layout property of the ContentView in the cell to Inferred (Constraints). Switching it back to Autoresizing Mask fixed the issue.
in IOS 15, if you have this setting set the wrong way, your app will crash
In my case, I was loading a nib file with a UITableViewCell contained in a UIView. After disabling autolayout in the nib file, I haven't got the exception again.
I also ran into this issue and it was due to a UITableViewCell subclass being used in the view hierarchy outside of a UITableView as noted in the other answers. In this case I had limited time, so I couldn't move the functionality present in that subclass into a UIView subclass. The workaround I came up with was to just create a UIView instance in my view hierarchy where the cell was supposed to be and transplant the view hierarchy of the cell instance into it, and remove the cell itself from the displayed view hierarchy. So long as I kept the views I moved wired to the various properties of the cell instance, everything keeps working fine.
It's a bit hacky, but it will keep things running until the time to refactor is available.
iOS 15 / Xcode 13
This crash happened to me when a .xib file had a UITableViewCell which was NOT nested in a UITableView. The cell was sitting inside a stackview - at one point it was more or less a placeholder so that some interface warnings would go away. I deleted the orphan table view cell & there was no more crash.
PEOPLE, THIS IS NOT A BUG. As never said down below:
"This is NOT bug, you simply cannot use a UIView as cell for table view, you must make sure the top level view in the nib file is a UITableViewCell."
I have tested it and it works.
You need to create the Xib file and delete the top-level view that it has. After, you drag and drop a UITableViewCell into the nib. This way, this problem will not happen.
Unselecting 'use auto layout' option from the .xib's properties resolved my issue.
- Posting what I did as this may help others.
Thank you.
In my case i was using a UITableViewCell in a UIView to create a custom cell
then I deleted the cell from the UIView and then added the all the contents in the UIView directly and that solved my problem.
Hope it helps you too.
All the best
In My case I had copied and pasted the cell, but constraints were missing that resulted in this error.
if you are copying the cell into another xib file, ensure constraints are also copied or create them again
In iOS15 / Xcode13, you can experience a crash with this error message if you inadvertently assign a custom UITableViewCell class to something that shouldn't be a UITableViewCell.
In my case, I had accidentally applied TastingCardInstructions – a subclass of UITableViewCell – to both the tastingCardInstructions cell and also to TastingCard, a view within its hierarchy (below).
TastingCard is a UIView, not a cell and therefore it cannot subclass UITableViewCell.
This built and ran fine before upgrading to Xcode13, but the upgrade exposed an old mistake.
In my case it was much simpler. Running perfectly with Xcode 12.5 but got the error in 13.1.
I had UITableViewCell and an extra view in the contentView. The extra view host a StackView and has as usual the default setting for the constraint 'Inferred (Constraints)'. There must be a change because now the constraint is named 'Inferred (Autoresizing Mask)'. Similar to the nonsense they did to the UIButton.
Changing 'Inferred (Constraints)' to 'Autoresizing Mask' solves my problem.
In my case the UITableViewCell parent was the main view instead of the UITableView. In this case you just have to put the cell inside the tableview.
I think this could be a common issue if you drag the cell into the view without taking care of the correct hierarchy.

iPhone- how to use Storyboard with custom UITableView cells and CellWithIdentifier

I'm trying to make use of the new Storyboard designer and the ease of drawing UITableView cells instead of programmatically "designing" each row.
A good example of a standard cell is linked here at stack overflow:
iPhone - dequeueReusableCellWithIdentifier usage
Instead of the linked approach of using "[cell textLabel]" I want to use my own labels, buttons and images on top of each row.
Therefore I created several prototype cells/rows and assigned identifiers to them (using Storyboard).
Now: what is the smartest way of accessing each row's "custom" controls/labels? I tried searching for accessing them by ID, but didn't find anything.
My thinking was it should work along this lines:
[[[cell subviews] getObjectByID:#"labelTime"] setText:#"Whatever"];
Is my expectation of the APIs completely wrong or didn't I just find the right API, yet?
Any ideas or recommendations?
For each new custom UITableViewCell that you create in storyboard, you will want to create a new class file which implements UITableViewCell to link it to. Be sure to map all of the controls within your new cell that you laid out in storyboard to instance properties. Then you'll just use it with dequeuing like normal:
YourTableViewCellClass *cell = (YourTableViewCellClass*)[tableView dequeueReusableCellWithIdentifier:#"YourCellIdentifierStringDefinedInStoryBoard"];
// then set the properties for the class.
cell.labelTime = #"whatever";
There are two ways you can get to your custom subviews. The simpler way is using tags. Every view has a tag property which is an integer. You can set the tag in the nib, and set or get it in code. You can search a view tree for a view with a given tag by sending viewWithTag: to the root of the tree. So, for example, you could give your labelTime view the tag 57, and in your code, you'd find the view like this:
UILabel *label = (UILabel *)[cell viewWithTag:57];
The downside of using tags is that you have to keep the tag numbers in sync between your nib and your code. If they get out of sync, you'll either get the wrong view back or you'll get nil (and since you can send messages to nil, the system won't give you an error when that happens). Still, tags are so convenient that it's pretty common to use them like this.
The other way is to create a custom subclass of UITableViewCell with an IBOutlet property for each custom subview. You can hook up the outlets to the subviews in the nib, and access the subviews via the properties in your code:
MyTableViewCell *myCell = (MyTableViewCell *)cell;
UILabel *label = cell.labelTime;
This entails writing a lot more boilerplate than using tags, but it has the advantage that you will get warnings or errors (either at compile-time or when you first try to load the nib) if your nib and your code get out of sync.
Don't be a doofus like I was being. :)
Make sure you set the Identifier value in the Attributes Inspector of the custom cell. Setting the Restoration ID of the custom cell in the Identity Inspector is the wrong way to go, and that's exactly what I did. Major facepalm for myself.
For more implementation details of how to do it, using the methods described by mservidio and rob, check out the section "Designing Our own Prototype Cells" in this tutorial.

How to connect an IBOutlet from an UITableViewController directly to custom cell?

A few days ago, I watched the video tutorial which explains how to use custom cells in an UITableViewController. I've learned that I can prepare a custom cell directly in the interface builder, so I did following:
I created a UITableViewController and connect a custom class which consists of an IBOutlet (UILabel). After that, I switched in my storyboard and prepared my custom cell with an UILabel. Finally I connect the label from UITableViewController to my custom cell directly.
The following happens:
Couldn't compile connection: <IBCocoaTouchOutletConnection:0x400724860 <IBProxyObject: 0x4007872c0> => productLabel => <IBUIImageView: 0x401080220>>
Isn't it possible to connect this directly? In the tutorial video of WWDC 2011 Section (Session #309) they do exactly what I did. But there is a hook: they don't show the code behind, they just connect it like I explained above.
For a better understanding I add a screenshot which shows, what i connect:
I had what turned out to be the same problem in this question. As the answerer of my question said, the problem is that the cell here is a prototype cell. A connection between a cell element and UITableViewController works fine for a table view with static cells since they are created at launch, but it doesn't make sense for prototype cells since many of them will probably be created...and they don't exist until cellForRowAtIndexPath is called. (This is a really poor error message, and Xcode probably shouldn't let you make a illegal connection like this.)
Actually,
I have seen the video and they are not connecting the way you suggest. They are connecting from the cell to the UILabel in the cell. In other words, they have a subclass of UITableViewCell and are connecting those IBOutlets in the custom object. In its basic form, you can create a custom cell class and just declare the interface and synthesize the properties and you should be good to go. Just make sure you set up your identifier and custom class correctly in the storyboard.
Hope this helps.

iphone copy/paste menu not shown in UITableViewCell

I placed a UITextField and a UITextView in a tableviewcell. When tap on text in them, it allows me to enclose a portion of the text with a rectangular popup showing the enclosed text enlarged. I want to either copy or paste over the selected text, but the copy-paste menu never show up whether I single tap, double taps or press and hold. I appreciate any hints you can give.
Somehow the copy/paste menu starts showing up when I tap the textfield or textview. Not clear on what I have done that made the difference. But I can tell you what I did for your reference. I tried to married NIB file, Custome UIViewController, UITableViewCell into one. I had the textfield and textview embeded in a UIViewController. The UIViewController userinterface is loaded from a NIB file. I then have the UIViewController view added as a subview into the tableViewCell. You can do some googling on this. There are many suggestions.
I do agree with some people who advice not to load NIB into an UITableViewCell. In the end, I took the advice and coded the user interface entirely without the NIB file. I declared a subclass of UITableViewCell then had thd user interface constructed within the initWithStyle. This way the custom tableViewCell was readily useful in the tableView without the headache of figuring out how to load the NIB file and how to make the UIViewController reuseable as a tableViewCell.
Since the code was a lot cleaner, I probably accidentally removed the bad code that had broke the copy/paste mechanism.