No matter how I seem to attempt to conquer this beast, it always beats me down. I cannot (without errors) have a UIImageView inside my UITableViewCell class. I've tried connecting it from the storyboard to the code through a referencing outlet with a strong and a weak storage, writing out the code myself, and even doing without the storyboard and creating a UIImageView programmatically. Is this just a bug with Swift? Am I doing something wrong?
Here's a screenshot of my error:
The UITableViewCell class already has a property by the name of imageView. This is why you cannot create another property with the same name.
Try using the already available image view or create an UIImageView instance with a different variable name.
Check the UITableViewCell documentation for more information
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableViewCell_Class/#//apple_ref/occ/instp/UITableViewCell/imageView
It is not a bug in swift and yes you are doing something wrong. A UITableViewCell class already has certain objects in it which can not be overridden. One of those is imageView. Since the base class of UITableViewCell already has an imageView object you are getting an error since you named your object the same thing. Just change the name to something else that is not part of the base class and it will work. Same thing goes for textLabel, detailTextLabel...etc.
Related
I am unable to make an #IBOutlet to my custom class from the elements in my prototype cell. I tried dragging from the element to the class; I let go of the blue line, and nothing happens.
Here are my files if you want to poke around.
Thanks in advance!
This is the issue:
Changing to this should fix this problem:
sometimes, some xcode tricks 'n hacks work, for example:
removing class name in storyboard for a cell;
restarting xcode;
cleaning project;
removing outlets and trying to prepare them again;
adding outlets using various techniques (ctrl+drag from storyboard, drag from an outlet defined in class file, drag from outlets tab - 'add new outlet')
removing cell class inheritance, then adding outlet, then adding inheritance back again
Make sure the CustomtableViewCell class is a Cocoa Touch Class file and not a swift file
I have created a custom view (Quantity View) with nib file in Swift. I have created some IBOutlets & IBActions (for buttons, labels etc.) in my custom view.
I tried to use this custom view (Quantity View) by assigning class name to a UIView in my storyboard.
It's showing me all the IBOutlets & IBActions in the Connections Inspector, as shown in this screenshot: .
I just want to show only delegate for the Custom view.
Possible Answer:
I thought I can use the -viewWithTag to get the views instead of Outlets.
But, I want to know if it's possible with having Outlets also or if there is much better way to do this?
What are the other possible ways (optimum) to handle this situation?
You can also consider the following solution:
You can take the subviews of your QuantityViews(custom view) and you can identify the specific views by its frame origin.
Note : you should know the customview subviews frame
Its not possible to hide IBOutlets from storyboard if you declare the class members as IBs (IBOutlets or IBActions).
The IBOutlets or the IBActions are just indicators to the interface builder so that it can show the names on it when you try to bind them it actually calls the setValue: forKey: method to set the view's reference to the IBOutlet property.
Now if you try to access an subview from the file's owner class without any IBoutlets you need to have a pointer to point it, so for that either you can get the reference using ObjectID which is assigned to the subview by the interface builder or you can get it using the viewWithTag: method.
The ObjectID you need to find all time when you add or replace a subview from the view, so better and convenient approach is to use tag property of UIView class.
So my conclusion to this problem is to access the views using the viewWithTag method you mentioned earlier.
I think your way is correct. But sometimes Xcode doesn't work correctly.
The following makes the IBOutlets and IBActions reappear and work properly:
Clean project your project in Xcode.
Quit Xcode completely.
Delete all contents of ~/Library/Developer/Xcode/DerivedData/.
Restart MacOS just in case.
I hope you will resolve that :)
Before you tell me that all I need to do is import UIKit, I know all about importing, and NO, that's not what I need to do in this case. Intrigued? Confused? Read on...
I have two different, but similar, custom UITableViewCells. Both have a UILabel and a UISwitch. Version one, DisplayCell, has a second UILabel, while version two, EditCell, has a UIPickerView. How I use them is like this, in a static UITableView that I'm using as a fill-in-the-data form, DisplayCell is the standard view which displays the selected value. The user can tap on DisplayCell to replace it with EditCell, then use the UIPickerView to pick a new value and hit done (button in the nav bar at the top). DisplayCell is then brought back, displaying the newly selected value. In either version the user can tap the switch to toggle whether or not the value from the UIPickerView should be used elsewhere in the form.
When it came time to write the code for the tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> (UITableViewCell) function I decided to rewrite my code so that I have a single class, SwitchCell that inherits from UITableViewCell and contains the IBOutlet and IBAction for the switch, then have DisplayCell and EditCell inherit from SwitchCell.
This works fine, however DisplayCell is now nothing more than a IBOutlet for a UILabel, and UITableViewCell, which DisplayCell inherits from via SwitchCell already has two UILabel IBOutlets, textLabel and detailTextLabel. The whole purpose in creating SwitchCell was to try and minimize code by only ever write any block of code once, something that I'm a bit of a fanatic about. Thus I would very much rather have the UILabel in DisplayCell use the detailTextLabel IBOutlet from UITabelViewCell, rather than having to create a 'redundant' UILabel IBOutlet for it.
In order to link to an IBOutlet in a superclass you must be able to bring up the code for that superclass in the assistant editor. Then you can just control+drag and link like normal. Which means, in theory, I believe it should be possible to link my UILabel to UITableViewCell's detailTextLabel, if I can access the appropriate line from the source code for UITableViewCell in the assistant editor. Is this possible, and if so, how?
Oh, and I'm working exclusively in Swift in this project, FYI.
tl;dr: It's really not anymore efficient to use UITableViewCell's default UILabels than it is to just add your own.
In order to link to an IBOutlet in a superclass you must be able to bring up the code for that superclass in the assistant editor.
Well, that's not correct. The assistant editor is not required to link to an IBOutlet defined in a superclass. But that's not important here anyway.
You can't link to detailTextLabel in Interface Builder because it's not defined with the #IBOutlet attribute. And although it is possible to override properties in Swift (so you can add #IBOutlet to it), that won't work in this case because an IBOutlet has to be mutable, and the superclass has defined the variable as immutable.
Now you could overcome this by adding your own setter method in the subclass to make the property mutable. I was able to do this with the following code:
var _detailTextLabel: UILabel?
#IBOutlet override var detailTextLabel: UILabel? {
get {
return super.detailTextLabel
}
set {
_detailTextLabel = newValue
}
}
I could wire this up in Interface Builder just fine. So perhaps I could tweak this code to actually get an set what I want (I don't think it would work as shown here). We're so far outside the realm of common-sense coding that we just need to stop and give up on this idea.
Thus I would very much rather have the UILabel in DisplayCell use the detailTextLabel IBOutlet from UITabelViewCell, rather than having to create a 'redundant' UILabel IBOutlet for it.
I can relate. I'm just as anal. :-)
However, if you do a little testing (or read the UITableViewCell header file) you'll see that UITableView is smart enough (optimized enough?) to not actually add a UILabel to the content view unless you try to use it. So at worst you have an unused property.
So it's really not inefficient to just add your own UILabel and property.
It's been awhile since I've used Xcode and I resumed an old project I was working on awhile ago. I created a new UITableViewCell subclass. In my storyboard, I dragged a UITableViewCell onto my UITableView of my UIViewController. I changed the type of the UITableViewCell to my subclass, but when I control + drag from the UITableViewCell subclass to the UITextField, it doesn't allow me to make the connection.
.h of my UITableViewCell custom subclass
#property (nonatomic, weak) IBOutlet UITextField *titleTextField;
I must be going crazy because I thought this was something that just worked. I saw in another UIViewController that there is a custom subclass for the UITableView when I worked on this project last. I changed the subclass that was having the problem to that type of UITableViewCell subclass and I'm still not able to ctrl+drag to make the IBOutlet connection. Am I missing something here? Wasn't this something that always worked this way? Is there something new I'm not aware of? I tried using the assistant editor as well to drag it to the code, but that doesn't work either. I went back to the UITableViewCell subclass that DOES have a connection already made from when I last worked on this, and I tried ctrl+dragging to the label again, and it doesn't bring up the menu on which outlet I want to connect to either.
Another thing that is weird, is when I'm trying to type the custom class of my UITableViewCell that is already created, even though I built my project, it doesn't autocomplete it in the Class field. I'm not sure if my Xcode is having problems. Also, I don't know if this matters, but in my UITableViewCell, I Have some standard UITableViewCells as well. Any thoughts? Thanks.
It looks like all I had to do for my class name to appear in the drop down was to quit Xcode and come back in. I thought things like that would have been fixed in the IDE.
I found that I could still add the connections in the connections inspector. I don't know why they removed the very easy ctrl+drag from the left hand side of the storyboard unless I'm missing something.
I have a custom UICollectionViewCell with an imageView, a textField and a label. I have a skeleton made in Interface Builder. I need to do some more work on the imageView (setting the image and giving it rounded corners) during the initialization process, but I can't do it during initWithCoder: because the outlets aren't set yet. I would like to do that work within the cell class so I can keep those components as private. Is there anyway I can do the work on those components during the initialization process, or do I need to make them public so that my collection view data source can do that work during cellForItemAtIndexPath:?
Do it awakeFromNib instead of initWithCoder. The outlets wil be set by then.