【问题标题】:How to expand cell when button inside cell is pressed?按下单元格内的按钮时如何扩展单元格?
【发布时间】:2020-03-03 08:00:37
【问题描述】:

我是 xcode 和 swift 的新手,我找到了在按下整个单元格时展开单元格的指南。 现在我想在按下单元格内的按钮时展开单元格。

我的视图控制器中有这个:

//datasource
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if tableView.tag == 100 {
        return nameArr.count
    }else{
        return prova.count
    }
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {


    if tableView.tag == 100 {

        let cell = tableView.dequeueReusableCell(withIdentifier: "MainTableViewCell") as! MainTableViewCell
        cell.lblName.text = nameArr[indexPath.row]
        cell.expand.tag = indexPath.row
        return cell
        }else{
        tableView.estimatedRowHeight = 60
        tableView.rowHeight = UITableView.automaticDimension
            let cell = tableView.dequeueReusableCell(withIdentifier: "InsideTableViewCell") as! InsideTableViewCell
                cell.lblInsideName.text = prova[indexPath.row]
                return cell
            }

}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    //se la cella è selezionata e deve essere tirata ancora giù ritorna 243 che sarebbe il valore della cella + l'immagine dentro da mostrare
    if  selectedIndex == indexPath.row && isCollapsed == true && tableView.tag == 100 {
            return 375
        }else {
            //altrimenti se è gia collassata e ripremiamo sulla cella ritorna 50 e quindi richiude la cella
            return 96
        }
}


//delegate
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {


    tableView.deselectRow(at: indexPath, animated: true)
    if selectedIndex == indexPath.row {
        if self.isCollapsed == false {
            self.isCollapsed = true
        }else{
            self.isCollapsed = false
        }
    }else{
        self.isCollapsed = true
    }
    self.selectedIndex = indexPath.row
    //tableView.reloadRows(at: [indexPath], with: .automatic)
    tableView.beginUpdates()
    tableView.endUpdates()
}

但我不知道该怎么做。

在我的 viewController 现在我有这个变量:

var selectedIndex = -1 //tell me which cell i pressed
var isCollapsed = false // tell if the cell is already collapsed 

【问题讨论】:

  • 你想从单元格按钮切换你的isCollapsed吗?
  • 是的,当按下单元格内的按钮时,我想折叠单元格
  • stackoverflow.com/questions/60226353/… ,可以在cellForRowAt中获取cell的buttonclick。检查这个。 & 将增加单元格高度的逻辑应用到回调闭包中(在 cellForRowAt 方法中)。
  • 这是所有表格单元格还是只是特定单元格?

标签: ios swift


【解决方案1】:

您可以在单元格中定义一个clouser,并在您按下单元格中的按钮时调用它

在您的单元格中定义:

var buttonClicked: (() -> Void)?

在你的单元格按钮中调用这个clouser

func buttonPressAction() {
   buttonClicked?()

}

在您的 cellForRow 方法中更改如下:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    if tableView.tag == 100 {

        let cell = tableView.dequeueReusableCell(withIdentifier: "MainTableViewCell") as! MainTableViewCell
        cell.buttonClicked = { [weak self] in
            if self.isCollapsed == false {
                self.isCollapsed = true
            } else{
                self.isCollapsed = false
            }
        }else{
            self.isCollapsed = true
        }
    }
}

【讨论】:

  • 我必须在哪里归还我的手机?
  • 你的单元格的其他东西和你的代码一样,我没有实现所有的代码,只是显示示例,其余的按照你的代码做
【解决方案2】:

修改数据源以改变它

当然数据源可以更复杂,它可以是一个包含是否打开属性的对象

let nameArr:[Bool] = [false, false, false]

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    self.nameArr[indexPath.row] = !self.nameArr[indexPath.row]
    tableView.reloadRows(at: [indexPath], with: .none)
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    let isshow = self.nameArr[indexPath.row]
    if isshow {
        return 375
    } else {
        return 69
    }
}

【讨论】:

    【解决方案3】:

    您应该在更改高度后更新表格视图: tableView.reloadData().

    您可以通过使用委托将有关该操作的信息发送到视图控制器来实现。

    1) 您应该保存单元格的状态。您可以将其存储在单元模型中:

    class YourCellModel {
        var isExpanded = false
    }
    

    2) 你应该创建一个委托协议:

    protocol YourCellDelegate: AnyObject {
        func buttonPressed()
    }
    

    3) 为单元格委托和单元格模型添加属性。另外你应该添加一个函数buttonPressed:

    class YourCell: UITableViewCell {
    
        weak var delegate: YourCellDelegate?
        var model: YourCellModel?
        //...
    
        @IBAction func buttonPressed() {
            model?.isExpanded = true
            delegate?.buttonPressed()
        }
    
    }
    

    4) 您应该将单元模型存储在视图控制器中:{

    class YourViewController: UIViewController {
    
        var cellModels: [YourCellModel] = []
        //...
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            cellModels = Array(repeating: YourCellModel(), count: <count of cells, maybe it is a prova.count>)
        }
    
    }
    

    5) 设置单元模型并委托给cellForItem 中的单元:

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let model = cellModels[indexPath.row]
        if tableView.tag == 100 {
            let cell = tableView.dequeueReusableCell(withIdentifier: "MainTableViewCell") as! MainTableViewCell
            cell.lblName.text = nameArr[indexPath.row]
            cell.expand.tag = indexPath.row
            cell.model = model
            cell.delegate = self
            return cell
        } else {
            tableView.estimatedRowHeight = 60
            tableView.rowHeight = UITableView.automaticDimension
            let cell = tableView.dequeueReusableCell(withIdentifier: "InsideTableViewCell") as! InsideTableViewCell
            cell.lblInsideName.text = prova[indexPath.row]
            cell.model = model
            cell.delegate = self
            return cell
        }
    }
    

    6) 更新heightForItem

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        let model = cellModels[indexPath.row]
        if model.isExpanded {
            return 375
        } else {
            return 96
        }
    }
    

    7) 你的视图控制器应该实现YourCellDelegate:

    extension YourViewController: YourCellDelegate {
        func buttonPressed() {
            tableView.reloadData()
        }
    }
    

    【讨论】:

    • 但这会折叠我表格视图中的所有单元格
    • @matteoantonucci 是的,您可以为此使用委托。我会用一个例子来更新我的答案。
    • 我用视图控制器中的代码更新问题,当按下该行中的按钮时,如何传递我必须展开的行?
    • 对不起,我不明白我必须在按下的 func 按钮内放什么?
    • @matteoantonucci 我添加了一个示例。更改高度后,您应该更新表格视图。
    猜你喜欢
    • 1970-01-01
    • 2018-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-13
    • 1970-01-01
    • 2011-11-22
    • 1970-01-01
    相关资源
    最近更新 更多