wouter
(wouter)
1
Hi all. I managed to load my custom cell inside my UITableView. Inside each table element i have button but when i click this nothing happends.
Normally i add this to a button:
h.addTarget(self ,action: selector(mybutton), for: .touchUpInside)
and then call a function
@objc func mybutton(){
let menu = MenuController()
present(menu, animated: true, completion: nil)
}
This doesn't do anything though. What am i doing wrong here?
class CustomCell: UITableViewCell {
var parent: String?
var maintit: String?
var comments: String?
var parentView : UILabel = {
var textView = UILabel()
textView.translatesAutoresizingMaskIntoConstraints = false
textView.backgroundColor = orangeBanner
textView.textColor = .white
textView.heightAnchor.constraint(equalToConstant: 50.0).isActive = true
return textView
}()
var mainView : UITextView = {
var textView = UITextView()
textView.translatesAutoresizingMaskIntoConstraints = false
textView.backgroundColor = grayBanner
textView.isScrollEnabled = false
textView.heightAnchor.constraint(equalToConstant: 100.0).isActive = true
return textView
}()
var commentView : UITextView = {
var textView = UITextView()
textView.translatesAutoresizingMaskIntoConstraints = false
textView.isScrollEnabled = false
textView.backgroundColor = UIColor.white
textView.heightAnchor.constraint(equalToConstant: 30.0).isActive = true
return textView
}()
let editInt: UIButton = {
let h = UIButton()
h.setTitle("Wijzigen", for: .normal)
h.setTitleColor(UIColor.white, for: .normal)
h.backgroundColor = orangeBanner
h.contentHorizontalAlignment = UIControl.ContentHorizontalAlignment.left
h.titleLabel?.font = h.titleLabel?.font.withSize(10)
h.addTarget(self ,action: #selector(mybutton), for: .touchUpInside)
h.widthAnchor.constraint(equalToConstant: 50.0).isActive = true
return h
}()
lazy var stackView2: UIStackView = {
let sv = UIStackView(arrangedSubviews: [parentView, editInt])
sv.translatesAutoresizingMaskIntoConstraints = false
sv.axis = .horizontal
sv.spacing = 0
return sv
}()
lazy var stackView: UIStackView = {
let sv = UIStackView(arrangedSubviews: [stackView2, mainView, commentView])
sv.translatesAutoresizingMaskIntoConstraints = false
sv.axis = .vertical
sv.spacing = 0
return sv
}()
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?){
super.init(style: style, reuseIdentifier: reuseIdentifier)
self.addSubview(stackView)
stackView.leftAnchor.constraint(equalTo: self.leftAnchor).isActive = true
stackView.rightAnchor.constraint(equalTo: self.rightAnchor).isActive = true
stackView.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
stackView.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true
}
override func layoutSubviews() {
super.layoutSubviews()
if let parent = parent {
parentView.text = parent
}
if let maintit = maintit {
mainView.text = maintit
}
}
required init?(coder aDecoder: NSCoder){
fatalError("init(coder:) has not been implemented")
}
@objc func mybutton(){
print("clicked")
}
}
UITable element on viewcontroller
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = self.intTitleTable2.dequeueReusableCell(withIdentifier: cellID) as! CustomCell
cell.parent = "\(mydata[indexPath.row].parent!) > \(mydata[indexPath.row].maintit!)"
cell.maintit = mydata[indexPath.row].comment
cell.comments = ""
//cell.layoutSubviews()
return cell
}
wouter
(wouter)
2
Never mind i understand now. have to be in my ViewController not inside my Customcell:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = self.intTitleTable2.dequeueReusableCell(withIdentifier: cellID) as! CustomCell
cell.parent = "\(mydata[indexPath.row].parent!) > \(mydata[indexPath.row].maintit!)"
cell.maintit = mydata[indexPath.row].comment
cell.comments = ""
cell.editInt.addTarget(self ,action: #selector(mybutton), for: .touchUpInside)
//cell.layoutSubviews()
return cell
}
@objc func mybutton(){
print("hello")
}