Subclassing UITableViewCell and connecting outlets - ios

I'm working in Swift 3 and Xcode 8.
I have a number of table view cell prototypes where the cells are very similar, but there are enough differences where I want to make a subclass of UITableviewCell to hold all the similar outlets and the basic initialization, but then create a few classes which subclass off of the new class.
Question is, how do I get the outlets for these cells all connected to the outlets in the base class?
I can do this for the first cell, but then IB sees those outlets as connected, and won't let me connect them from the next subclassed cell.
I copied the first prototyped cell and pasted it in, then changed it's class to the second type. This still showed the outlets connected, and this seems to work, however, an action for a button in the second cell is not working.
What's the trick to getting all the different subclassed cells to have their outlets and actions connected to the base cell class?

If you have different classes for different cell then take objects of those classes and by using them you will be able to use your different cell's label,button etc
let cell1 = tableView.dequeueReusableCellWithIdentifier("Cell1",forIndexPath: indexPath) as! YourSubClass1
let cell2 = tableView.dequeueReusableCellWithIdentifier("Cell2",forIndexPath: indexPath) as! YourSubClass2

You need to remove the outlets of the second cell and connect them again to the subclass outlet properties after changing the second class type.
If you don't remove the outlets, it will still refer to the outlets of the first cell and that won't work.

Related

Connecting label to UITableViewCell

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)

Unable to access custom table cell variables

I'm using Xcode 6.0.1, with Swift. I have a Table View which was working fine for a normal type of cell, but it's started displaying errors after I changed it to use a custom cell. I made a Table View Cell in the storyboard, made a Cocoa Touch Class file, MyCustomCell, which is a subclass of UITableViewCell, and set the custom cell in the storyboard to use this class, in the Identity inspector. I wired a text field I put into the custom cell in the storyboard to the MyCustomCell.swift file as a variable called someData.
The problem occurs here:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell") as MyCustomCell
cell.someData!.text = "testing"
return cell
}
Come runtime, the "testing" line gives me a fatal error: unexpectedly found nil while unwrapping an Optional value error, assuming that I manually registered the cell class in viewDidLoad, like this:
tableView.registerClass(MyCustomCell.self, forCellReuseIdentifier: "Cell")
If, instead, I never register the class, instead putting "Cell" in the storyboard as the Restoration ID for the custom cell, I get the same exact error nil error message, but on the "let cell =" line instead of the "testing" line.
The MyCustomCell class wires the someData variable and has init, awakeFromNib, and setSelected shells but nothing else.
I'm not really sure how to fix this. Maybe I need to create a nib for the custom cell or something? Any help would be appreciated.
(By the way, I'm using Core Data to store entities for each table cell, but that's not shown here, since I'm only concerned with getting custom cells working right now.)
You are using the Restoration Id (Identity Inspector) instead of the Indentifier (Attributes Inspector) in Interface Builder.
When UITableView is instantiated from storyboard calling
registerClass(_:forCellReuseIdentifier:) causes problems. You need to give reueseIdentifier in storyboard in attributes inspector.

How to reuse custom UITableViewCell defined as a prototyped cell in Storyboard controller

I have the only cell template for items at two different UITableViewControllers/TableViews.
What I need is to define it once and then reuse at other UITableView via
UITableView.DequeueReusableCell(CellId);
The issue is that is when I call this method on UITableView which doesn't contain cell prototype I'm getting NULL.
How to reuse my prototyped cell across multiple table controllers?
I want to define cell template in storyboard, NOT xib.
It turned out that the only way to reuse a cell it to design it with xib and register at tableview that xib with cellid.
Just copy-paste your prototype cell in every table view controller where you need it.
And if I understand your question, in a standard and proper way, it's not possible to dequeue a cell from another table view, Apple implementation handles this mechanism itself.
Using xib for a reusable cell is beneficial while cell design is fixed in the whole app. But when there are conditional requirements or slight changes in design or functionality and remaining design and functionality is same for tableview cell, in this case if you still want to reuse the code then you can subclass tableview cell class.

Strange error when adding items to prototype cells in storyboard-IB

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.

Storyboards: How to add outlets to a prototype UITableViewCell?

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.

Resources