如何从导航栏更改 UITableViewCell?
How to make changes to UITableViewCell from navigationBar?
如何从 UITableViewController 的导航栏更改 UITableViewCell 的布局?感觉好像什么都试过了,还是没用。
这是控制器:
class TableViewController: UITableViewController {
let tableViewCellID = "tableViewCellID"
//MARK: Loading view
override func viewDidLoad() {
super.viewDidLoad()
tableView.register(TableViewCell.self, forCellReuseIdentifier: tableViewCellID)
navigationBar()
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 100
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let tableViewCell = tableView.dequeueReusableCell(withIdentifier: tableViewCellID) as? TableViewCell else { return UITableViewCell() }
tableViewCell.firstLayout()
return tableViewCell
}
func navigationBar() {
let emptyButton = UIBarButtonItem(image: UIImage(named: "icon"), style: .done, target: self, action: #selector(buttonTapped))
navigationItem.rightBarButtonItems = [emptyButton]
navigationItem.largeTitleDisplayMode = .never
}
@objc func buttonTapped() {
let tableViewCell = TableViewCell()
tableViewCell.secondLayout()
}
}
这是单元格:
class TableViewCell: UITableViewCell {
let firstColorView: UIView = {
let coverView = UIView()
coverView.translatesAutoresizingMaskIntoConstraints = false
coverView.backgroundColor = .red
return coverView
}()
let secondColorView: UIView = {
let cameraViewClosure = UIView()
cameraViewClosure.translatesAutoresizingMaskIntoConstraints = false
cameraViewClosure.backgroundColor = .yellow
return cameraViewClosure
}()
func firstLayout() {
contentView.addSubview(firstColorView)
NSLayoutConstraint.activate([
firstColorView.widthAnchor.constraint(equalTo: contentView.widthAnchor, multiplier: 1),
firstColorView.heightAnchor.constraint(equalTo: contentView.heightAnchor, multiplier: 1),
])
}
func secondLayout() {
print("This prints, but no changes are made to the cell")
contentView.addSubview(secondColorView)
NSLayoutConstraint.activate([
secondColorView.widthAnchor.constraint(equalTo: contentView.widthAnchor, multiplier: 0.5),
secondColorView.heightAnchor.constraint(equalTo: contentView.heightAnchor, multiplier: 1),
secondColorView.centerXAnchor.constraint(equalTo: contentView.centerXAnchor),
])
}
}
这些我当然都试过了:
layoutIfNeeded()
setNeedsLayout()
setNeedsDisplay()
layoutSubviews()
setNeedsUpdateConstraints()
updateConstraints()
updateConstraintsIfNeeded()
...我已经尝试了各种组合,但我仍然没有得到我想要的结果。
如果您需要更多代码或信息,请告诉我,在此先感谢您的帮助!
尝试以下修改。
@objc func buttonTapped() {
let cell = tableView.visibleCells.first as? TableViewCell
cell?.secondLayout()
}
如何从 UITableViewController 的导航栏更改 UITableViewCell 的布局?感觉好像什么都试过了,还是没用。
这是控制器:
class TableViewController: UITableViewController {
let tableViewCellID = "tableViewCellID"
//MARK: Loading view
override func viewDidLoad() {
super.viewDidLoad()
tableView.register(TableViewCell.self, forCellReuseIdentifier: tableViewCellID)
navigationBar()
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 100
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let tableViewCell = tableView.dequeueReusableCell(withIdentifier: tableViewCellID) as? TableViewCell else { return UITableViewCell() }
tableViewCell.firstLayout()
return tableViewCell
}
func navigationBar() {
let emptyButton = UIBarButtonItem(image: UIImage(named: "icon"), style: .done, target: self, action: #selector(buttonTapped))
navigationItem.rightBarButtonItems = [emptyButton]
navigationItem.largeTitleDisplayMode = .never
}
@objc func buttonTapped() {
let tableViewCell = TableViewCell()
tableViewCell.secondLayout()
}
}
这是单元格:
class TableViewCell: UITableViewCell {
let firstColorView: UIView = {
let coverView = UIView()
coverView.translatesAutoresizingMaskIntoConstraints = false
coverView.backgroundColor = .red
return coverView
}()
let secondColorView: UIView = {
let cameraViewClosure = UIView()
cameraViewClosure.translatesAutoresizingMaskIntoConstraints = false
cameraViewClosure.backgroundColor = .yellow
return cameraViewClosure
}()
func firstLayout() {
contentView.addSubview(firstColorView)
NSLayoutConstraint.activate([
firstColorView.widthAnchor.constraint(equalTo: contentView.widthAnchor, multiplier: 1),
firstColorView.heightAnchor.constraint(equalTo: contentView.heightAnchor, multiplier: 1),
])
}
func secondLayout() {
print("This prints, but no changes are made to the cell")
contentView.addSubview(secondColorView)
NSLayoutConstraint.activate([
secondColorView.widthAnchor.constraint(equalTo: contentView.widthAnchor, multiplier: 0.5),
secondColorView.heightAnchor.constraint(equalTo: contentView.heightAnchor, multiplier: 1),
secondColorView.centerXAnchor.constraint(equalTo: contentView.centerXAnchor),
])
}
}
这些我当然都试过了:
layoutIfNeeded()
setNeedsLayout()
setNeedsDisplay()
layoutSubviews()
setNeedsUpdateConstraints()
updateConstraints()
updateConstraintsIfNeeded()
...我已经尝试了各种组合,但我仍然没有得到我想要的结果。
如果您需要更多代码或信息,请告诉我,在此先感谢您的帮助!
尝试以下修改。
@objc func buttonTapped() {
let cell = tableView.visibleCells.first as? TableViewCell
cell?.secondLayout()
}