我是ios的新手我正在使用表格视图制作一个项目,其中我已经扩展了具有4个内容视图的表格视图单元格 . 当表格仅加载第一个内容视图时,当我选择行时,所有内容视图都会展开 . 但是当我点击第四个内容视图时,我也想要扩展所有内容视图单元格后,我想在第四个视图的tapGESTUREAction方法中访问indexPath.row . 这个扩展Cell的代码包含4个内容视图func tableView(_ tableView:UITableView,numberOfRowsInSection section:Int) - > Int {print(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 回答
在
heightForRowAt indexPath
中,您使用的是两个return
. 第一个将执行 . calculated height 方法无法执行 . 并且不需要tableView.dequeueReusableCell
. 该功能只需要单元格的高度 .例:
并且您还可以使用动态单元格高度,在_1553589中:
这里不需要
heightForRowAt indexPath
方法 . 高度将自动计算 .