Reputation: 81
Okay so I'm having a UIPicker custom cell and I would like to pass the selected item on "didSelectRow" to another custom cell, I tried doing this with Delegates but the problem is I can't set the delegate as the receiver custom cell(to the one that I want to receive the data), so.. here's my code:
UIPickerView Cell:
import UIKit
protocol AlbumsPickerCellDelegate {
func didSelectedAlbum(_ selectedAlbum: String)
}
class AlbumsPickerTableViewCell: UITableViewCell {
// var indexPath: IndexPath!
@IBOutlet var albumsPicker: UIPickerView!
var pickerData = ["Album1", "Album2", "Album3"]
var albumsPickerCellDelegate: AlbumsPickerCellDelegate?
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
self.albumsPicker.delegate = self
self.albumsPicker.dataSource = self
}
class func cellHeight() -> CGFloat {
return 162.0
}
}
extension AlbumsPickerTableViewCell: UIPickerViewDelegate, UIPickerViewDataSource {
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return pickerData.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return pickerData[row]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
print(pickerData[row])
//TODO: Pass the selected data to the albumCell to update the label:
let selectedAlbumOption = pickerData[row]
print("Selected Item: \(selectedAlbumOption)")
if let delegate = albumsPickerCellDelegate {
delegate.didSelectedAlbum(selectedAlbumOption)
}
}
}
The other custom cell(that I want the data to pass to):
import UIKit
class AlbumCell: UITableViewCell, AlbumsPickerCellDelegate {
func didSelectedAlbum(_ selectedAlbum: String) {
DispatchQueue.main.async {
self.chosenAlbumLabel.text = selectedAlbum
}
}
@IBOutlet var albumTitleLabel: UILabel!
@IBOutlet var chosenAlbumLabel: UILabel!
var albumsPickerTableViewCell = AlbumsPickerTableViewCell()
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
albumsPickerTableViewCell.albumsPickerCellDelegate = self
}
func configureCell(choosenAlbum: String) {
// albumTitleLabel.text = text
chosenAlbumLabel.text = choosenAlbum
}
}
Upvotes: 0
Views: 389
Reputation: 164
It doesn't work because in AlbumCell
you create another instance of AlbumsPickerTableViewCell
and use it for delegation.
Generally speaking, implementing a delegate from one cell to another doesn't feel right. You can quickly find yourself in the situation when you need the value from the first cell in the controller and have no way of obtaining it. Also, a strange behaviour can occur if those cells are going to be reused by table view.
In your case, it worth making UIViewController
which contains UITableView
a delegate of AlbumsPickerTableViewCell
, and when it gets called from the cell, pass data into AlbumCell
.
Also, don't forget that references to the delegate should be weak
to prevent strong reference cycle.
Upvotes: 2