In my viewcontroller,
I added my tableview (#IBOutlet weak var MyTableView: UITableView!) in which I added several custom cells with specific size depending on the elements needed inside.
I call each cell with identifier :
func tableView(_ MyTableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.row == 0 {
let cell = MyTableView.dequeueReusableCell(withIdentifier: "FirstCell") as! FirstCell
self.SettingsTableView.rowHeight = 220
return cell
} else if indexPath.row == 1 { etc etc...
And at a specific row, I've added a UIswitch directly in the cell that should expand / collapse this cell depending if it's ON or OFF.
I'd like to make it work a bit like the SelectRowAtIndexPath method and animate it to make appear 3 textFields below....
I'm looking for hours a solution, if you have any idea... Any suggestion is welcome !
If your switch is properly hooked up to the cell, the problem may be that you didn't refresh the UITableView, and thence it doesn't expand.
Related
I'm trying to make a UITableView that can support having different objects/elements inside it. Specifically, these elements are a UITextField and UISwitch.
The first problem:
The elements do not show up. They are placed in the prototype cell, which is then constructed inside the class I have set up. I have verified that the cell setup is working because I can change the words on each cell, but there are no elements within the cell.
Here is the code that constructs my cells right now:
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
return 1
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "EmailCell")
return cell
}
Second problem (which might be solved along with the first):
I have no way of accessing the information in each UITextField or each UISwitch. How can I get this information from all cells that exist?
Thanks in advance for the help!
There are multiple things wrong with your code.
For custom cells you need to implement a custom UITableViewCell subclass. Here is an example:
import UIKit
class EmailCell: UITableViewCell {
#IBOutlet var customTextField: UITextField!
#IBOutlet var customSwitch: UISwitch!
}
After that, open your Storyboard and select the prototype cell. Change it's class to EmailCell in the Identity Inspector.
Also make sure to connect your ui elements to the #IBOutlets created earlier. See this StackOverflow post if you need help with #IBOutlet.
In the next step, change your tableView(_:, cellForRowAt:) implementation like this:
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
let cell = tableView.dequeueReusableCell(withIdentifier: "EmailCell", for: indexPath) as! EmailCell
// Configure your custom ui elements however you want
cell.customTextField.text = "somestring"
cell.customSwitch.isOn = true
return cell
}
Make sure your cells have reuse identifiers and you're using
tableView.dequeueReusableCell(withIdentifier: -Your cell Id- , for: indexPath) as? -Your Cell Class-
in your cell for row at index datasource method
next you can add targets to your cell text field / switch by doing this in your cell for row at index datasource method
cell.-your switch / text field-.addTarget(self, action: #selector(valueChanged(_:)), for: .valueChanged)
and you should subclass a uitableview cell to add the property / iboutlets
class YourTableViewCell: UITableViewCell {
#IBOutlet weak var yourSwitch: UISwitch!
}
I would like to select the first cell in the tableview and give it an alpha of 100%, only for the first cell and this style should not be reused by the dequeueReusableCell method.
if indexPath.row == 0 {
cell.imageView.alpha = 1.0
}
When I use above code within the function cellForItemAt it will be set alpha of 1.0 to every 7th cell. I only want it to be applied on the first cell and the style should not be reused... How can I solve this? Should I create another custom cell or is there a kind approach in code do achieve this?
You should reset the imageView's alpha if it's not the first cell.
if indexPath.row == 0 {
cell.imageView.alpha = 1.0
}
else {
cell.imageView.alpha = 0.0 // or whatever alpha value you need
}
Create a UITableViewCell xib subclass. Then you can register the same cell with different reuseIdentifiers when you're setting up your table.
tableView.register(CustomCell, forCellReuseIdentifier: "AlphaCell")
tableView.register(CustomCell, forCellReuseIdentifier: "{REUSE_ID}")
When you setup your cell in:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
dequeue "AlphaCell" for your first cell, and "{REUSE_ID}" for your other cells.
I've created a custom UITableView Cell and everything seems to be working with one exception. I have a UISwitch that is inside the cell (hooked up to it's own UITableViewCell class that the tableView loads) but it only appears when you click on the cell or the cells background is clear/transparent. Ideally I have a white background for the cell and the switch on top of the background.
I've tried some hacky stuff like:
cell.bringSubview(toFront: cell.switch)
and
cell.switch.isHidden = false
But that obviously didn't work.
The switch is enabled and ON by default.
The tableview and switch is created from storyboards.
The hierarchy looks like this - TableView > Cell > Content View > Switch
Here's a video to see in detail - http://quick.as/rpyub8mv
Xcode Storyboard Screenshot
Custom TableViewCell Class
class SettingsBoolCell: UITableViewCell {
#IBAction func switchAction(_ sender: UISwitch) {
}
#IBOutlet weak var switchOutlet: UISwitch!
}
ViewController Implementation
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "settingsSectionOne", for: indexPath) as! SettingsBoolCell
switch indexPath.section {
case 0: cell.textLabel?.text = titles[0]
case 1: cell.textLabel?.text = titles[1]
case 2: cell.textLabel?.text = titles[2]
default: ()
}
return cell
}
If you're using storyboard, make sure that your UISwitch is inside the right view, and that it is under the other components in your document outline.
If you're generating the view inside the cell programmatically, make sure that you add the UISwitch to the right view with addSubView last. You can also set zPositions with view.layer.zPosition attribute.
So I was setting -
cell.textLabel.text?
to change the text of the cells inside the tableView. The problem is, apparently when you are using a custom cell, you can't access the default cell properties without some funky behavior.
Thanks everyone for your help!
I have a UITableView, and I'd like a tap in cell X to cause the label text in cell Y to change from "Waiting..." to "Done". I'm pretty baffled as to how I should go about this. I've tried to do this using didSelectRowAtIndexPath , but seemingly I can only modify properties of the cell at the row tapped on.
Thanks!
Its quite easy to get a cell you need to change, but you have to know which row of your table it is:
class MyCell : UITableViewCell {
#IBOutlet weak var myLabel: UILabel!
}
class TableViewController: UITableViewController {
...
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let row = 7 //or what you set
let cellWhereIsTheLabel = tableView.cellForRow(at: IndexPath(row: row, section: 0)) as! MyCell
cellWhereIsTheLabel.myLabel.text = "Done"
}
...
}
A solution will be to update the datasource of your tableview.
When user taps on cell, didSelectRowAtIndexPath is called. In this function you can update datasource to provoke changes in other cells and then call reloadData to apply changes.
When you call reloadData tableView is updated by cellForRowAtIndexPath. This function uses your datasource to update cell.
How can I achieve this screen with UITableViewCell and UITableViewController. With table section and header. Some ideas to achieve this?? Thanks!
What have you tried so far?
Your question seems a little broad.
You will need a set of custom UITableViewCell Subclasses, which you design in nibs.
To make the cells seem apart from each other, resize the content size of the Cells, and make the cell background another color.
Create a Segmented Control and add it to the Tableviews HeaderView.
For the FooterView it seems like this is some kind of subclassed Tabbar.
Easiest way to customise it in such a way, would be to create a View, and add buttons to it. Add this View as Subview to your TableViewController.
Have 2 UITableViewCell's one for each type i.e. 1 for showing the image and text and another for showing just the text.
Then in the cellForRowAt delegate method determine which to type to use based off the object you are data binding it to.
Example:
public final func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let customObject = customObjects[indexPath.section]
switch customObject.type {
case .imageAndText:
if let cell = tableView.dequeueReusableCell(withIdentifier: ImageAndTextCell.identifier, for: indexPath) as? ImageAndTextCell {
cell.customObject = customObject
return cell
}
case .text:
if let cell = tableView.dequeueReusableCell(withIdentifier: TextCell.identifier, for: indexPath) as? TextCell {
cell.customObject = customObject
return cell
}
}
return UITableViewCell()
}