快速单击表视图单元格之一内的按钮后重新加载表视图 [英] Reload tableview after clicking the button present inside one of the tableview cell in swift

查看:21
本文介绍了快速单击表视图单元格之一内的按钮后重新加载表视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含单元格数量的表格视图.每个单元格都有一个按钮.通过单击按钮,将打开一个下拉框.我希望当我单击特定单元格的按钮时,特定单元格的下拉框将显示,而不同单元格的所有其他下拉列表将被隐藏.

I have a tableview with numbers of cell. Each cell has a button. By clicking the button a dropdown box will open. I want when I will click on the button of a particular cell the dropdown box of the particular cell will show and all other dropdown of different cells will be hidden.

我的代码是

@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
            }
            
        })
    }
}

获取索引路径

  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
}

用于更新我的 tableview 的代码

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() }
    }
}

在我的表格视图 cellFor row at 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 是一个变量

Here buttonTag is a variable

var buttonTag = 0

var buttonTag = 0

我做错了什么?我认为 tableview 重新加载问题就在这里.

I am doing Anything wrong here? I thing the tableview reloading problem is here.

推荐答案

解决起来很简单,

每次单击按钮时,都会在控制器中记录索引,然后重新加载 tableView.

every time you click a button, you record the index in the controller, then reload the tableView.

在单元格配置中,如果单元格的indexPath与记录索引匹配,则显示其子表,隐藏其余子表.

in the cell configuration, if the cell's indexPath matches the record index, its sub table show ,the rest's sub table hide.

如果你按两次按钮,控制器中的记录索引为nil,然后重新加载tableView,所有cell的子表都隐藏了.

If you hit the button two times, the record index in the controller is nil, then reload tableView, and all cell's sub table hides.

我给图案命名,标记 &配置

I name the pattern, mark & config

你的代码很混乱

从你的代码来看,我认为 LcodeTableViewCell 是一个对象,而不是一个类.

from your code , I think LcodeTableViewCell is an object , not a class.

@IBAction func dropDown2ButtonAction(_ sender: UIButton){
    
    if buttonTag == getIndexPath()!.row{

buttonTag == getIndexPath()!.row,将永远为真.

//错误代码 ...

// BAD CODE ...

这篇关于快速单击表视图单元格之一内的按钮后重新加载表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆