如何在 swift 中的 tableview 中的 didselectrow 方法中点击内容视图

Posted

技术标签:

【中文标题】如何在 swift 中的 tableview 中的 didselectrow 方法中点击内容视图【英文标题】:how to tap in content view in didselectrow method in tableview in swift 【发布时间】:2017-07-06 04:34:13 【问题描述】:

我是 ios 的新手,我正在使用表格视图制作一个项目,其中我扩展了具有 4 个内容视图的表格视图单元格。加载表格时,仅显示第一个内容视图,当我选择行时,所有内容视野扩大。但我也希望在点击第四个内容视图时展开所有内容视图单元格后,我想访问第四个视图的 tapGESTUREAction 方法中的 indexPath.row。 这是我扩展 Cell 的代码,它由 4 个内容视图组成 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int 打印(YearArr.count) 返回 YearArr.count;

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 
        let cellIdentifier = "Cell"
        let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier) as! customCell
        cell.selectionStyle = .none
             cell.lblGrossSal.text = EarnArr[indexPath.row]

         cell.lblTotlSal.text = netPayArr[indexPath.row]
         cell.lblmonthhName.text = YearArr[indexPath.row]
            print(selectedIndex,indexPath.row)
        let height = cell.bounds.height
        print(height)
       return cell;
    

       func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat 
        let cellIdentifier = "Cell"
        let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier) as! customCell
        cell.selectionStyle = .none

        if(selectedIndex == indexPath.row) 

                       return 190;
            return self.calculateHeight(selectedIndexPath: indexPath)
         else 

                        return 50;
        
           
 // fourthView
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) 

        if(selectedIndex == indexPath.row) 
            selectedIndex = -1
                    else 
            selectedIndex = indexPath.row

        
        self.expandTableView.beginUpdates()
        //self.expandTableView.reloadRows(at: [indexPath], with: UITableViewRowAnimation.automatic )
        self.expandTableView.endUpdates()


       
    func calculateHeight(selectedIndexPath: IndexPath) -> CGFloat 
        let cell = self.expandTableView.cellForRow(at: selectedIndexPath) as! customCell
        cell.lblDeductSal.frame = self.cellSummaryLabelFrame(cell: cell, selectedIndexPath: selectedIndexPath)

        return 80 + 35 + 21 + 10 + cell.lblDeductSal.frame.size.height + 30
    

    func cellSummaryLabelFrame(cell: customCell, selectedIndexPath: IndexPath) -> CGRect 
        print(DeductionArr[selectedIndexPath.row])
        cell.lblDeductSal.text = DeductionArr[selectedIndexPath.row]
        cell.lblDeductSal.numberOfLines = 0
        var labelFrame = cell.lblDeductSal.frame
        let maxSize = CGSize.init(width: cell.lblDeductSal.frame.size.width, height: CGFloat.greatestFiniteMagnitude)
        let requiredSize = cell.lblDeductSal.sizeThatFits(maxSize)
        labelFrame.size.height = requiredSize.height
        return labelFrame
    

【问题讨论】:

【参考方案1】:

heightForRowAt indexPath 中,您使用了两个return。第一个将执行。 计算高度方法无法执行。并且不需要tableView.dequeueReusableCell。该函数只需要单元格的高度。

例子:

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat 
    if(selectedIndex == indexPath.row) 
        return self.calculateHeight(selectedIndexPath: indexPath)
     else 
        return 50;
    

您还可以在 viewDidLoad 中使用动态单元格高度:

tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 44 //Default cell height

这里不需要heightForRowAt indexPath 方法。高度会自动计算。

【讨论】:

先生,很好,但我的问题是如何获取当前从 didselectrow 方法中选择的行的 indexPath 值到 tapGesture 方法 如何点击当前选中行的内容视图并获取indexPath值 didSelectRowAt indexPath delegate 中,您将获得当前选定的 indexPath。如果你想要最后选择的 indexPath,你会从didDeSelectRowAt indexPathdelegate 得到它

以上是关于如何在 swift 中的 tableview 中的 didselectrow 方法中点击内容视图的主要内容,如果未能解决你的问题,请参考以下文章