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

时间:2017-07-06 04:34:14

标签: swift tableview cell uitapgesturerecognizer

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

答案 0 :(得分:0)

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方法。高度将自动计算。