0

I have a piece of code that has been running fine in it's view controller. I wanted to clean it up, so I added in a table view so that it would be more clean. I just created cells in the table view and drag and dropped my label and buttons. However now I have the error of

Outlets cannot be connected to repeating content 

which I know has been asked before, however I cannot get a specific answer on why this happens. Here is my view controller code where the labels are being connected. I know that there is the solution of making a subclass, but everything already works in the view controller so I was wondering if there was a more simple fix to this.

class CharViewController: UIViewController, BLEDelegate, MFMailComposeViewControllerDelegate, UIPickerViewDelegate, UIPickerViewDataSource, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var label: UILabel!
etc etc etc

And where the only thing I did was drag and drop from my view onto a table view cell.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
timedwalnut
  • 94
  • 1
  • 13
  • Please check this out: http://stackoverflow.com/questions/31550519/swift-outlets-cannot-be-connected-to-repeating-content – cpatmulloy Jan 03 '17 at 22:40

2 Answers2

1

Protoype cells are repeating so it will not work. You have two options:

1) Use Static Cells. These require a UITableViewController but they are not considered repeating content (static).

2) Create a custom UITableViewCell and connect the outlet there. You will then have to dequeue to cell as you custom tableView cell and adjust the label through that cell.

rmickeyd
  • 1,551
  • 11
  • 13
  • If I use static, will it work if the values can be changed anytime? For example a button that has a value of 1 can change values, and anytime the value is changed it sends data across to an external device. Will that still work? – timedwalnut Jan 03 '17 at 22:59
  • The static cell should work just like the ViewController option. It will just be a single instance of that label and/or button. If you are using the tableView strictly for formatting purposes it should work as you intend. – rmickeyd Jan 03 '17 at 23:01
1

That is because you may be trying to make outlet of objects that you have placed inside the cell prototype in you Storyboard. To get rid of this error in future do the following steps

  • Step # 1 Create an other class that is subclass of UITableViewCell, select your cell from the view hierarchy and assign this class to the cell from this option just like you assign a New ViewController class to a ViewController in Storyboard.

  • Step # 2 Make your IBOutlets in your custom cell class just like you were trying to create that outlet in your UIViewController and/or in any other class.

  • Step # 3 For Example you have created two IBOutlets named cellLabel and cellImageView now you can access these properties by simply type casting your cell object while dequeReusableCellWithIdentifier method like this.

    let cell=tableView.dequeueReusableCell(withIdentifier:"YourCellIdentifier", for:indexPath) as! YourCustomCellClassName;
    cell.cellLabel.text = "Any Thing";
    cell.cellImageView.image = UIImage(named: "MyImage.jpg");
    

Here is the image link that will show you how to Assign your class to your custom cell prototype. Image Is Here

Syed Qamar Abbas
  • 3,637
  • 1
  • 28
  • 52