Swift 如何从导航栏更改UITableViewCell?

Swift 如何从导航栏更改UITableViewCell?,swift,uitableview,uikit,Swift,Uitableview,Uikit,如何从UITableViewController的导航栏对UITableViewCell进行布局更改?感觉好像我什么都试过了,但都没用 这是控制器: 类TableViewController:UITableViewController{ 让tableViewCellID=“tableViewCellID” //标记:加载视图 重写func viewDidLoad(){ super.viewDidLoad() tableView.register(TableViewCell.self,forCel

如何从UITableViewController的导航栏对UITableViewCell进行布局更改?感觉好像我什么都试过了,但都没用

这是控制器:

类TableViewController:UITableViewController{
让tableViewCellID=“tableViewCellID”
//标记:加载视图
重写func viewDidLoad(){
super.viewDidLoad()
tableView.register(TableViewCell.self,forCellReuseIdentifier:tableViewCellID)
导航栏()
}
重写func tableView(tableView:UITableView,heightForRowAt indexath:indexPath)->CGFloat{
返回100
}
重写func tableView(tableView:UITableView,numberofrowsinssection:Int)->Int{
返回1
}
重写func tableView(tableView:UITableView,cellForRowAt indexath:indexPath)->UITableViewCell{
guard let tableViewCell=tableView.dequeueReusableCell(标识符为tableViewCellID)为?tableViewCell else{return UITableViewCell()}
tableViewCell.firstLayout()的
返回表视图单元格
}
func导航栏(){
让emptyButton=UIBarButtonItem(图像:UIImage(名为:“图标”),样式:。完成,目标:自我,操作:#选择器(按钮映射))
navigationItem.rightBarButtonItems=[emptyButton]
navigationItem.largeTitleDisplayMode=.never
}
@objc func按钮标记(){
设tableViewCell=tableViewCell()
tableViewCell.secondLayout()
}
}
这是单元格:

class TableViewCell:UITableViewCell{
让firstColorView:UIView={
让coverView=UIView()
coverView.translatesAutoresizingMaskIntoConstraints=假
coverView.backgroundColor=.red
返回封面视图
}()
让secondColorView:UIView={
让cameraViewClosure=UIView()
cameraViewClosure.TranslatesAutoResizengMaskintoConstraints=false
cameraViewClosure.backgroundColor=.yellow
返回摄影机关闭
}()
func firstLayout(){
contentView.addSubview(firstColorView)
NSLayoutConstraint.activate([
firstColorView.widthAnchor.constraint(等式:contentView.widthAnchor,乘数:1),
firstColorView.heightAnchor.constraint(等式:contentView.heightAnchor,乘数:1),
])
}
func secondLayout(){
打印(“打印,但不更改单元格”)
contentView.addSubview(secondColorView)
NSLayoutConstraint.activate([
secondColorView.widthAnchor.constraint(等式:contentView.widthAnchor,乘数:0.5),
secondColorView.heightAnchor.constraint(等式:contentView.heightAnchor,乘数:1),
secondColorView.centerXAnchor.constraint(等同于:contentView.centerXAnchor),
])
}
}
我当然尝试过所有这些:

layoutIfNeeded()
setNeedsLayout()
setNeedsDisplay()
布局子视图()
setNeedsUpdateConstraints()
updateConstraints()
updateConstraintSifRequired()
。。。我尝试了各种组合,但仍然没有得到我想要的结果


如果您需要更多代码或信息,请告诉我,并提前感谢您的帮助

尝试以下修改

@objc func buttonTapped() {
    let cell = tableView.visibleCells.first as? TableViewCell
    cell?.secondLayout()
}

精彩的!非常感谢。您是否有可以链接到的文档?以下是相关文档: