单击swift中一个tableview单元格内的按钮后重新加载tableview
我有一个有许多单元格的表格视图。每个单元格都有一个按钮。单击该按钮将打开一个下拉框。我想当我点击某个特定单元格的按钮时,该单元格的下拉框会显示出来,而其他所有不同单元格的下拉框都会隐藏起来 我的代码是单击swift中一个tableview单元格内的按钮后重新加载tableview,swift,uitableview,Swift,Uitableview,我有一个有许多单元格的表格视图。每个单元格都有一个按钮。单击该按钮将打开一个下拉框。我想当我点击某个特定单元格的按钮时,该单元格的下拉框会显示出来,而其他所有不同单元格的下拉框都会隐藏起来 我的代码是 @IBAction func dropDown2ButtonAction(_ sender: UIButton){ if buttonTag == getIndexPath()!.row{ if isdropDownSelected2{ i
@IBAction func dropDown2ButtonAction(_ sender: UIButton){
if buttonTag == getIndexPath()!.row{
if isdropDownSelected2{
isdropDownSelected2 = false
}
else{
isdropDownSelected2 = true
}
}
else{
if let index = getIndexPath(){
buttonTag = index.row
}
isdropDownSelected2 = false
delegate?.updateTableView(completion: { [self] (updated) in
if updated{
isdropDownSelected2 = true
}
})
}
}
获取indexath
func getIndexPath() -> IndexPath? {
guard let superView = self.superview as? UITableView else {
print("superview is not a UITableView - getIndexPath")
return nil
}
let indexPath = superView.indexPath(for: self)
return indexPath
}
显示或隐藏我的tableView的代码
var isdropDownSelected2 = false{
didSet{
if isdropDownSelected2 == true{
showTable2()
}
else{
hideTable2()
}
}
}
private func showTable2(){
tableView2.isHidden = false
}
private func hideTable2(){
tableView2.isHidden = true
}
func updateTableView(completion: @escaping (Bool) -> Void) {
tableView.reloadData {
completion(true)
}
}
extension UITableView {
func reloadData(completion:@escaping ()->()) {
UIView.animate(withDuration: 0, animations: { self.reloadData() })
{ _ in completion() }
}
}
更新我的tableview的代码
var isdropDownSelected2 = false{
didSet{
if isdropDownSelected2 == true{
showTable2()
}
else{
hideTable2()
}
}
}
private func showTable2(){
tableView2.isHidden = false
}
private func hideTable2(){
tableView2.isHidden = true
}
func updateTableView(completion: @escaping (Bool) -> Void) {
tableView.reloadData {
completion(true)
}
}
extension UITableView {
func reloadData(completion:@escaping ()->()) {
UIView.animate(withDuration: 0, animations: { self.reloadData() })
{ _ in completion() }
}
}
在我的表视图中,在indexpath函数中为行设置单元格
LcodeTableViewCell?.dropDownBox2.tag = indexPath.row
LcodeTableViewCell?.delegate = self
if LcodeTableViewCell?.tableView1.isHidden == false{
LcodeTableViewCell?.tableView1.isHidden = true
}
if LcodeTableViewCell?.tableView2.isHidden == false{
LcodeTableViewCell?.tableView2.isHidden = true
}
这里buttonTag是一个变量
var buttonTag=0
我做错什么了吗?我想tableview的重新加载问题就在这里。解决这个问题非常简单 每次单击按钮时,都会在控制器中记录索引,然后重新加载tableView 在单元配置中,如果单元的indexPath与记录索引匹配,则显示其子表,隐藏其余的子表 如果按两次按钮,控制器中的记录索引为
nil
,然后重新加载tableView,所有单元格的子表都将隐藏
我将模式命名为mark&config
您的代码很混乱
@IBAction func dropDown2ButtonAction(_ sender: UIButton){
if buttonTag == getIndexPath()!.row{
if isdropDownSelected2{
isdropDownSelected2 = false
}
else{
isdropDownSelected2 = true
}
}
else{
if let index = getIndexPath(){
buttonTag = index.row
}
isdropDownSelected2 = false
delegate?.updateTableView(completion: { [self] (updated) in
if updated{
isdropDownSelected2 = true
}
})
}
}
从您的代码中,我认为LcodeTableViewCell
是一个对象,而不是一个类
@IBAction func dropDown2ButtonAction(_ sender: UIButton){
if buttonTag == getIndexPath()!.row{
buttonTag==getIndexPath()!。行
,将始终为真
//错误代码