-1

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!

TheMooCows237
  • 233
  • 3
  • 18
  • Is the style of the prototype cell set to `custom`? – vadian Feb 20 '17 at 18:18
  • You have to show the code that you are using to "construct the cells inside the class". Also, a screenshot of your Storyboard may be helpful. – naglerrr Feb 20 '17 at 18:20
  • It seems like you're trying to create a form. You can try out https://github.com/xmartlabs/Eureka – Sweeper Feb 20 '17 at 18:31
  • @vadian Yes it is set to custom – TheMooCows237 Feb 21 '17 at 00:08
  • @TheMooCows237 can you also show your `EmailCell` implementation? – naglerrr Feb 21 '17 at 06:36
  • @naglerrr What exactly do you mean by that? `EmailCell` is currently just the reuse identifier for the prototype cell, and there is no code associated with it. (besides the declaration in the `cellForRowAt` method) – TheMooCows237 Feb 21 '17 at 07:23
  • Ok, so that is your problem. You need a `UITableViewCell` subclass `EmailCell`. I just added an answer explaining how to do that. – naglerrr Feb 21 '17 at 08:23

2 Answers2

1

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.

enter image description here

Also make sure to connect your ui elements to the @IBOutlets created earlier. See this StackOverflow post if you need help with @IBOutlet.

enter image description here

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
}
Community
  • 1
  • 1
naglerrr
  • 2,809
  • 1
  • 12
  • 24
  • This works fantastically! Thank you so much. One thing, do you know how I would detect a change in the text field or switch within my _view controller_ class? (This is the class that contains tableView setup) – TheMooCows237 Feb 21 '17 at 23:07
0

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!

}
Gaston Gonzalez
  • 427
  • 3
  • 6
  • Okay, I added the dequeueReusableCell to inside the cellForRowAt method. However, I cannot call cell.[switch/ text field] because it doesn't recognize that there is a text field inside the prototype cell. I also cannot link the text field as an outlet because XCode gives me an error saying that I cannot link outlets to repeating content. – TheMooCows237 Feb 21 '17 at 00:18