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)
Related
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.
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 created an xcode project with the master detail template. I want to customize the cell to put my data into 2 separate labels. I tried customizing the cell in the storyboard editor, but it seems like all of the stuff on the cell is locked in place. I am fine either creating the custom cell programmatically or in the storyboard editor. I know I could just set the text on the default textLabel to blank and then create the other labels programmatically, but that doesn't seem very need to just have a random empty textLabel in the middle of every cell. So I am wondering if there is a way to edit the cell in storyboard editor, delete the default textLabel, or resize and reposition the textLabel to where I need it to go.
Any help is greatly appreciated.
Thanks in advance.
That's standard practice. Just ignore the built in textLabel and be done with it. By default the label is blank anyway. It's set in code.
To access new labels and such, create a new custom class for the custom cell (File|New|File...|Objective-C Class in XCode), give it a name and for the subclass choose UITableViewCell. Finally, on the Identity Inspector for the custom cell in Interface Builder, choose that new class for the Custom Class.
Now, from the Assistant Editor in XCode, you can CTRL-drag new labels and such to the .h file of the new custom cell class to create IBOutlet properties.
Be sure to set the cell Identifier on the Attributes Inspector tab for the cell and reference that in your code, especially in cellForRowAtIndexPath.
Import the custom cell class's .h file in the view controller's .h file, then cast the cell to your custom cell class in cellForRowAtIndexPath to access the new properties or change the definition UITableViewCell *cell = to your new class like MyTableViewCell *cell =.
By default, the Style of the table cell is "Basic" in Master-Detail projects. Select the Table View Cell and open the Attributes Inspector. Change the Style from Basic to Custom.
Doing this removes the default label and allows you to add new controls.
I have quite a large project (~20 scenes). One of which is a TableViewController with a custom UITableViewController class. I have given the cell a reuse identifier, and added a label to it. When I try and Ctrl+Drag the label to the UITableViewController header file to create an outlet, I get the following error:
error: Illegal Configuration: Connection "tableInfoView" cannot have a
prototype object as its destination.
What is this? Am I overlooking something obvious? Or do I need to create a custom cell class and drag the outlet to that? If so, how do I then specify the data which is displayed uniquely for each cell from the UITableViewController?
In fact you can't just make an outlet from a dynamic cell prototype in the UITableView delegate view controller.
You'll have to subclass UITableViewCell and then attribute this class to your prototype.
Then you can Ctrl-Drag from the Label to the UITableViewCell subclass header file.
Finaly you can access to this outlet in the delegate code after having imported the UITableViewCell header file in it.
This is documented by Apple there at "The Technique for Dynamic Row Content" section.
or you could give the label a tag (e.g. 100) and use
myLabel = [myTableView viewForTag:100];
to get the label
I had the same error myself. Just to add one more potantial root cause for future readers:
In my case I copied a control (a Button in this case) from one prototype cell to the next and the action still referred to the neighbor cell. My table has several different prototype cells.
The fact, that it acutally was a proper subclass of UITableViewCell which was properly connected to the prototype cell made it difficult to actually see the mistake.
Tag the label and you can reach the label anywhere in the viewcontroller like with viewWithTag from the table view.
UILabel *destinationLabel = (UILabel *)[self.tableView viewWithTag:1];
destinationLabel.text = #"Label Destaination";
I faced the same problem but later it turned out that it was just a silly mistake.
I mistakenly dragged the label from Cell to my controller's #interface
This could be your problem too. just cross check once.
Set the right reuse identifier used in .m file in the Storyboard for the Prototype cell.I had the same situation and this helped me
After doing every thing right if problem still exist then just removed all outlets and rejoin them carefully and it worked very fine for me.
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.