Swift PickerView with custom tableViewCell.xib

766 Views Asked by At

I am messing around with swift and i am stuck in creating an UIPickerView connection with a custom TableViewCell.xib. The functionality i want is the following: when i press the switch button a pickerView appears that lets me select a month which will appear instead of the "Select Month" label. I don't really understand where should i create the outlets.

Cell Xib

Main StoryBoard

class TableViewCell: UITableViewCell {

@IBOutlet weak var leftImage: UIImageView!
@IBOutlet weak var label: UILabel!
@IBOutlet weak var rightImage: UIImageView!



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
}

func commonInit(_ imageName1: String, _ imageName2: String, text: String){
    leftImage.image = UIImage(named: imageName1)
    rightImage.image = UIImage(named: imageName2)
    label.text = text
}

}

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {


let textData = ["Select Month"]

let datePicker = UIDatePicker()

@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
    super.viewDidLoad()

    self.title = "UiTableView"

    tableView.delegate = self
    tableView.dataSource = self

    let nibName = UINib(nibName: "TableViewCell", bundle: nil)
    tableView.register(nibName, forCellReuseIdentifier: "tableViewCell")
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
   return textData.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "tableViewCell", for: indexPath) as! TableViewCell
    cell.commonInit("facebook", "twitter", text: textData[indexPath.item])
    return cell
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 170

}

func createDatePicker(){
    let toolBar = UIToolbar()
    toolBar.sizeToFit()

    let doneButton = UIBarButtonItem(barButtonSystemItem: .done, target: nil, action: nil)
    toolBar.setItems([doneButton], animated: false)


}


override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

}

1

There are 1 best solutions below

1
On

Open up the Cell Xib file and choose from the Menu, View>Assistand Editor>Show Assistant Editor. Once the Assistant Editor is open you can open the TableViewCell class. Control Drag from the 3 controls in the XIB in Interface builder to the corresponding entry in the file. Once over the entry such as @IBOutlet weak var leftImage: UIImageView!, it will highlight. Release the mouse and the connection will be established.

It is actually a little bit easier to not type the line with the outlet declaration in the class first. In this case, when you control drag from ID you position the mouse over an empty line and a dialog box appears where you can declare the outlet. It creates the connection at the same time.