Add UISwitch to subtitle style of tableviewCell - ios

I want to use subtitle style of table view cell (not the custom one), and also I need to have an UISwitch in the right side of the cell.
if the name of the cell is TitleCell
I know I can add a UISwitch programmatically to it by using this line of code:
TitleCell.accessoryView = UISwitch()
But can I have access to this UISwitch and using it as an outlet and giving it an action?
Thanks for your help in advance

You can give an action to the switch programmatically by calling addTarget (see UISwitch: Swift 3: Programmatically ).
Also consider moving to a custom cell xib/class. It is not hard to set up, but then you have full control over what is there and how it is laid out.

After assigning the switch to the accessory view you can access it with
if let mySwitch = TitleCell.accessoryView as? UISwitch {
// it's the switch, you can use it.
}
However you are encouraged to design a custom cell with an outlet by subclassing UITableViewCell
And please conform to the naming convention that variable names start with a lowercase letter.

Related

StoryBoard actions cannot be targeted at repeating content [duplicate]

I have just created an app and have started hooking up #IBOutlet's to the storyboard. I am connecting some of them to labels in a UITableViewCell Prototype Cell with a Basic Style. When I connect it though I get this error in the Storyboard:
The detailText Outlet from the TableViewController to the UILabel is invalid. Outlets cannot be connected to repeating content.
Can someone help me out? I have set it up the way I always do successfully but this time it has chucked me this error.
Create a table view cell subclass and set it as the class of the prototype. Add the outlets to that class and connect them. Now when you configure the cell you can access the outlets.
There are two types of table views cells provided to you through the storyboard, they are Dynamic Prototypes and Static Cells
1. Dynamic Prototypes
From the name, this type of cell is generated dynamically. They are controlled through your code, not the storyboard. With help of table view's delegate and data source, you can specify the number of cells, heights of cells, prototype of cells programmatically.
When you drag a cell to your table view, you are declaring a prototype of cells. You can then create any amount of cells base on this prototype and add them to the table view through cellForRow method, programmatically. The advantage of this is that you only need to define 1 prototype instead of creating each and every cell with all views added to them by yourself (See static cell).
So in this case, you cannot connect UI elements on cell prototype to your view controller. You will have only one view controller object initiated, but you may have many cell objects initiated and added to your table view. It doesn't make sense to connect cell prototype to view controller because you cannot control multiple cells with one view controller connection. And you will get an error if you do so.
To fix this problem, you need to connect your prototype label to a UITableViewCell object. A UITableViewCell is also a prototype of cells and you can initiate as many cell objects as you want, each of them is then connected to a view that is generated from your storyboard table cell prototype.
Finally, in your cellForRow method, create the custom cell from the UITableViewCell class, and do fun stuff with the label
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "yourCellIdentifier") as! YourCell
cell.label.text = "it works!"
return cell
}
2. Static Cells
On the other hand, static cells are indeed configured though storyboard. You have to drag UI elements to each and every cell to create them. You will be controlling cell numbers, heights, etc from the storyboard. In this case, you will see a table view that is exactly the same from your phone compared with what you created from the storyboard. Static cells are more often used for setting page, which the cells do not change a lot.
To control UI elements for a static cell, you will indeed need to connect them directly to your view controller, and set them up.
If you're using a table view to display Settings and other options (like the built-in Settings app does), then you can set your Table View Content to Static Cells under the Attributes Inspector. Also, to do this, you must embedded your Table View in a UITableViewController instance.
Or you don't have to use IBOutlet to refer to the object in the view. You can give the Label in the tableViewCell a Tag value, for example set the Tag to 123 (this can be done by the attributes inspector). Then you can access the label by
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "someID", for: indexPath)
let label = cell.viewWithTag(123) as! UILabel //refer the label by Tag
switch indexPath.row {
case 0:
label.text = "Hello World!"
default:
label.text = "Default"
}
return cell
}
With me I have a UIViewcontroller, and into it I have a tableview with a custom cell on it. I map my outlet of UILabel into UItableviewcell to the UIViewController then got the error.
As most people have pointed out that subclassing UITableViewCell solves this issue.
But the reason this not allowed because the prototype cell(UITableViewCell) is defined by Apple and you cannot add any of your own outlets to it.
Sometimes Xcode could not control over correctly cell outlet connection.
Somehow my current cell’s label/button has connected another cell
I just remove those and error goes away.
For collectionView :
solution:
From viewcontroller, kindly remove the IBoutlet of colllectionviewcell
. the issue mentions the invalid of your IBOutlet. so remove all subclass which has multi-outlet(invalids) and reconnect it.
The answer is already mentioned in another question for collectionviewcell
Click on simulator ,
Navigate to Window and enable Device Bezels

illegal configuration the labelname outlet from the classname to the UILabel is invalid error in ios xcode

i am beginner of iOS and i am trying to display some array objects on the label. the label is located on the table cell. when i am going to hookup label and label object in the class then trowing the above error plz assist me .
Thank's.
If you want to connect your label to class, you must make a subclass of UITableViewCell and set the class of your cell to be this one. e.g. you have a cell in your storyboard, you must set the class of that cell (the custom class that you made) and then you can make the outlet..
You can also work with tags. In this case, you will not need outlets. You can get the label using tag. Let me know if it's still not clear or confusing
Another thing that you can do (if you only need label in your cell, then no need to make custom/prototype cell). You can simply do it using UITableViewCell and it has label in it. You can do this as follows (in Swift):
var cell = tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell!
cell.textLabel!.text = "Your text"
TO connect uilabel outlet you have to subclass UITableViewCell first. Then declare uilabel outlet in that class. Assign that class to your tableview cell in xib and then try to set outlet.

Set custom accessory type in cusom cell

I have a custom UITableViewCell, and I would like to add a custom accessory type to it. I can set the custom accessory type in the cellForRowAtIndexPath, but I don't think that is an ideal way.
Question:
How can I set a custom accessory type to a custom cell?
You can just assign a custom UIView (image,button, etc..) to the property
cell.accessoryView = <your custom view>;
If the custom accessoryView is different in every cell the in the delegate method
cellForRowAtIndexPath:
you can create the assignment, if it is the same in each cell then you can subClass a UITableViewCell (follow this tutorial Link)

Is it possible to select a ViewObject in TableViewCell Content

In other places, we can drag the object as Outlet in a ViewController, give it a name and use it as a reference later. But, In a table cell is it possible to select the Object through Code as:
var cell = tableView.dequeueReusableCellWithIdentifier("menuCell") as UITableViewCell
let contentView:UIView = cell.contentView;
//Can we achieve this?
let imgView = contentView.ImageView
Even though the subviews are added to the contentView, the outlets should be made to the cell itself. So you should have outlets in your cell's .h file, and connect them from the cell to the subview in IB. In code, you would refer to them with cell.label, etc.

Modify outlet inside UITableView custom cell

I have a custom UITableViewCell that has 2 UITextFields and a UIButton. The text fields are set in the cell:ForRow:AtIndexPath: method. What I need is to change the opacity of this UIButton dynamically, without having to reload the entire tableview. I was thinking of something like cellForRowAtIndexPath:, but how to access the outlet in this cell?
Thanks
When you create the cell in cellForRowAtIndexPath: just maintain a pointer to it in code. If that button is on every cell then you can just create an array of buttons so that you can easily access the button for the cell you want.
UIButton *cellButton = [[UIButton alloc] init];
[allCellButtons addObject:cellButton];

Resources