我的 contentView 中有三个类( SurePeople , MaybePeople , NopePeople )。它们都是相同的UITableView
PeopleTableView 的子类。唯一的区别是他们从何处获取用户数组。如果users.count等于零,则将tableView的heightAnchor常数更改为零以将其隐藏。
当我单击显示的第一个Tableview的第一个Tableview行时,将调用方法didSelectRowAt
,如果我单击任何其他行,则什么也不会发生。
发生了什么事?
contentView:
for controller in tableViewControllers {
contentView.addSubview(controller)
controller.delegateTVToSingleEvent = delegateSVToSingleEvent?.getSingleEventVC()
controller.delegateTVToScrollView = self
let heightCon = controller.heightAnchor.constraint(equalToConstant: 0)
heightCon.isActive = true
heightCons.append(heightCon)
}
PeopleTableView:
class PeopleTableView: UIView, ReusableView {
//MARK: - Properties & Variables
var delegateTVToSingleEvent: tableViewToSingleEvent?
var delegateTVToScrollView: tableViewToScrollView?
var myEvent: Event
var users: [User]! = [User]()
//MARK: - GUI Objects
let peopleTableView: UITableView = {
let tableview = UITableView()
return tableview
}()
let peopleLabel: UILabel = {
let label = UILabel()
label.font = UIFont.systemFont(ofSize: 20)
label.text = "Label:"
label.textColor = .black
return label
}()
//MARK: - Init & View Loading
init(frame: CGRect, event: Event) {
self.myEvent = event
super.init(frame: frame)
self.backgroundColor = LebensfitSettings.Colors.basicBackColor
setupTableView()
setupViews()
}
//MARK: - Setup
func setupTableView() {
peopleTableView.delegate = self
peopleTableView.dataSource = self
peopleTableView.register(TeilnehmerTVCell.self, forCellReuseIdentifier: TeilnehmerTVCell.reuseIdentifier)
peopleTableView.tintColor = .white
peopleTableView.isScrollEnabled = false
peopleTableView.allowsSelection = true
}
func setupViews() {
addSubview(peopleLabel)
addSubview(peopleTableView)
}
func removeViews() {
peopleTableView.removeFromSuperview()
peopleLabel.removeFromSuperview()
}
func confBounds(){
peopleLabel.anchor(top: topAnchor, left: leftAnchor, bottom: nil, right: rightAnchor, paddingTop: 15, paddingLeft: 0, paddingBottom: 0, paddingRight: 0, width: 0, height: 25)
peopleTableView.anchor(top: peopleLabel.bottomAnchor, left: leftAnchor, bottom: bottomAnchor, right: rightAnchor, paddingTop: 5, paddingLeft: 0, paddingBottom: 0, paddingRight: 0, width: 0, height: 0)
}
//MARK: - Methods
func loadUsers() {
setNeedsUpdateConstraints()
if users.count == 0 {
removeViews()
padding = 0
} else {
setupViews()
padding = 15+25+5
}
self.updateConstraints()
delegateTVToScrollView?.finishedLoadingParticipants()
}
//MARK: - Do not change Methods
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
extension PeopleTableView: UITableViewDataSource {
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let row = tableView.dequeueReusableCell(withIdentifier: TeilnehmerTVCell.reuseIdentifier, for: indexPath) as! TeilnehmerTVCell
//TODO: don't just pass the user.. fill the rows elements from here
row.isUserInteractionEnabled = true
row.user = users[indexPath.row]
row.confBounds()
return row
}
}
//MARK: - TVDelegate
extension PeopleTableView: UITableViewDelegate {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return users.count
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return height
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
guard let id = users[indexPath.row].uid else { return }
delegateTVToSingleEvent?.gotoProfile(clickedUID: id)
}
}