Reputation: 313
I'm making a table view app, and have created UITableViewCell programmatically. The cell consists of 2 views, the first one for the Label, and the second one is for 2 buttons and label. Pressing on the button should change the label's title. Also, I'm using delegates to implement logic in the view controller. Here is my cell code. Sorry for the mess with the constraints. I'm only learning to implement them nicely.
protocol FinalCellDelegate {
func addButtonPressed(sender: UIButton)
func subtactButtonPressed(sender: UIButton)
}
class FinalCell: UITableViewCell {
let label = UILabel()
var addButton = UIButton()
var numberLabel = UILabel()
var subtractButton = UIButton()
var delegate: FinalCellDelegate?
override func awakeFromNib() {
super.awakeFromNib()
}
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: "FinalCell")
contentView.addSubview(label)
let addAndSubtractView = UIView()
addAndSubtractView.addSubview(addButton)
addAndSubtractView.addSubview(numberLabel)
addAndSubtractView.addSubview(subtractButton)
contentView.addSubview(addAndSubtractView)
//constraints
addButton.translatesAutoresizingMaskIntoConstraints = false
addButton.trailingAnchor.constraint(equalTo: addAndSubtractView.trailingAnchor, constant: -15).isActive = true
addButton.leadingAnchor.constraint(equalTo: numberLabel.trailingAnchor, constant: 10).isActive = true
addButton.centerYAnchor.constraint(equalTo: contentView.centerYAnchor).isActive = true
subtractButton.translatesAutoresizingMaskIntoConstraints = false
subtractButton.leadingAnchor.constraint(equalTo: addAndSubtractView.leadingAnchor, constant: 10).isActive = true
subtractButton.trailingAnchor.constraint(equalTo: numberLabel.leadingAnchor, constant: -10).isActive = true
subtractButton.centerYAnchor.constraint(equalTo: contentView.centerYAnchor).isActive = true
numberLabel.translatesAutoresizingMaskIntoConstraints = false
numberLabel.centerYAnchor.constraint(equalTo: contentView.centerYAnchor).isActive = true
addAndSubtractView.translatesAutoresizingMaskIntoConstraints = false
addAndSubtractView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -5).isActive = true
addAndSubtractView.centerYAnchor.constraint(equalTo: contentView.centerYAnchor).isActive = true
label.translatesAutoresizingMaskIntoConstraints = false
label.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 20).isActive = true
label.centerYAnchor.constraint(equalTo: contentView.centerYAnchor).isActive = true
label.textColor = .black
subtractButton.setTitle("-", for: .normal)
subtractButton.setTitleColor(.black, for: .normal)
addButton.setTitle("+", for: .normal)
addButton.setTitleColor(.black, for: .normal)
numberLabel.text = "0"
numberLabel.textColor = .black
subtractButton.addTarget(self, action: #selector(subtractButtonTapped(_:)), for: .touchUpInside)
addButton.addTarget(self, action: #selector(addButtonTapped(_:)), for: .touchUpInside)
}
@objc func addButtonTapped(_ sender: UIButton) {
print("addButtonTapped")
delegate?.addButtonPressed(sender: sender)
}
@objc func subtractButtonTapped(_ sender: UIButton) {
print("subtractButtonTapped")
delegate?.subtactButtonPressed(sender: sender)
}
}
Here is my cellForRow at indexPath code :
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "FinalCell", for: indexPath) as! FinalCell
cell.delegate = self
cell.cellTag = indexPath.row
cell.addButton.tag = indexPath.row
cell.subtractButton.tag = indexPath.row
return cell
}
And of course delegate implementation :
extension FinalGameSettings: FinalCellDelegate {
func addButtonPressed(sender: UIButton) {
print("works")
}
func subtactButtonPressed(sender: UIButton) {
print("This works too")
}
}
The problem is, that when I press on Button - nothing happens. Please, give me a piece of advice to handle the problem!
Upvotes: 0
Views: 704
Reputation: 881
I copy your project and make my own. After debugging, i found this. The button in your view is not inside the view addAndSubtractView
. So that means you can't click it. And your addAndSubtractView
does not show in the cell, It's hidden somewhere.
po addButton.frame
▿ (60.0, -17.0, 30.0, 34.0)
▿ origin : (60.0, -17.0)
- x : 60.0
- y : -17.0
▿ size : (30.0, 34.0)
- width : 30.0
- height : 34.0
Try fixing your auto-layout. I suggest you try SnapKit framework for better looking
Upvotes: 1