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.
I want to make "tableViewcell.selection = .None" , either by storyboard or code with minimum lines. I have tricks to do it, but I really willing to know if there is any other easiest possible way exist which I may missing in the apple documentation.
I am working with below components:-
UITableViewController
UITableView (Static)
UITableViewCell (no IBOutlet connected because its not required for my simple implementation)
Subviews of UITableView has IBOutlets
In .swift, didSelectForRowAtIndexPath defined because I want user interaction on click (no delegate method called because so far not required)
Is there any way to disable cell selection from storyboard.
Set the selection style of cell to None in storyboard.
Refer this:
https://developer.apple.com/reference/uikit/uitableviewcell/1623221-selectionstyle
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.
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.
I have a tableView that's loosely based on the DetailViewController in ye olde SQLiteBooks. It's created programatically. Each cell displays a single value that when clicked displays a generic editingController that allows the user to edit that value. Pretty standard Cocoa-touch stuff...
Except...I also have a segmented control in the header of said tableView that depending on it's setting needs to change an attribute (textColor) on a UILabel in ONE of the 8± tableViewCells in the table. I have no IBOutlets for this tableView because I created it entirely in code. So, what do I need to do in my (void)segmentAction:(id)sender method (triggered when segmentedControl changes state) to allow me to access & change this value and display it to the user? When the table was built (cellForRowAtIndexPath) every UILabel was called "value" and then added: [cell.contentView addSubview:value].
I've tried setting a property of the viewController itself that is then checked during cellForRowAtIndexPath and does the textColor business there...and then adding [self.tableView setNeedsDisplay] to my segmentAction: method but it doesn't seem to work?!?
Any ideas?
If you know the indexpath of the cell you want to modify, you can call cellForRowAtIndexPath: to retrieve the UITableViewCell. Once you have that, you can get the UILabel from it. If you set the UILabel's tag to some useful value when create it, you can then retrieve it from the UITableViewCell via viewWithTag:.
In the class that implements cellForRowAtIndexPath you could also store a reference to the UILabel that you want when you build the relevant cell.
This would then mean you could alter the label and mark it to be repainted.
Alternately you could hold a local BOOL to show that the cell should be drawn differently - and then use this this in the cellForRowAtIndexPath: to draw it with a background.
You might find this easier with the 3.0 textLabel object in a UITableViewCell