0

After instantiating the EditCountsTableViewCell programmatically its outlets return nil when accessed, and I therefore cannot edit the cells. The cells behave normally when instantiated with the Storyboard.

@IBAction func countNameChanged(sender: AnyObject) {

    let textField = sender as! UITextField
    let text = textField.text

    let superview = textField.superview!
    let cell = superview.superview as! EditCountsTableViewCell
    let indexPath = self.tableView.indexPathForCell(cell)
    let row = indexPath?.row

    var newCell = EditCountsTableViewCell() //Instantiate Cell Programatically

    println(newCell)

    println(newCell.countName) //nil

    if let name = newCell.countName{

        name.text = text

    }

    if let value = newCell.countValue{

        value.text = cell.countValue.text

    }

    tableViewContents[row!] = newCell

}

EditCountsTableViewCell Class:

import UIKit

class EditCountsTableViewCell: UITableViewCell {

    @IBOutlet weak var countName: UITextField!
    @IBOutlet weak var countValue: UITextField!

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}

2 Answers2

1

Have you tried this way of initialization?

let storyboard = UIStoryboard(name: "MyStoryboardName", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("someViewController") as! UIViewController

(Source)

Community
  • 1
  • 1
Islam
  • 3,654
  • 3
  • 30
  • 40
  • I'm kind of new to this, how would instantiating the view controller programmatically help? – themaxgoldman Aug 21 '15 at 15:00
  • Usually if your desired view controller is in a different storyboard you use code to instantiate that, other than that try to use segue's. Less code = less bugs – Islam Aug 21 '15 at 15:32
  • But how would this help to instantiate a `TableViewCell`? – themaxgoldman Aug 21 '15 at 16:23
  • This would instantiate the `tableView` as if you accessed it from the storyboard, so the system would take care of instantiating all of its _dependents_ – Islam Aug 21 '15 at 17:37
0

You want to get a EditCountsTableViewCell variable programatically, but your cell is inited from storyboard, so you should get it from storyboard.

Siam
  • 162
  • 2
  • 9
  • So is there a way to properly instantiate an `EditCountsTableViewCell` programmatically? – themaxgoldman Aug 21 '15 at 14:35
  • You just want to edit the cell, why didn't you use func cellForRowAtIndexPath(_ indexPath: NSIndexPath) -> UITableViewCell? to get the cell? – Siam Aug 21 '15 at 14:47
  • I'm not just editing the cell, I am trying to keep a separate array of all of the cells in the tableview, I cannot find a way to simply access this from the `TableView` class – themaxgoldman Aug 21 '15 at 14:58