可扩展iOS tableViewCell
我有一个定制的tableViewControlelr,它有圆形的卡片视图单元格。tableViewCells还可以单击,单击时,它会展开可扩展iOS tableViewCell,ios,swift,uitableview,Ios,Swift,Uitableview,我有一个定制的tableViewControlelr,它有圆形的卡片视图单元格。tableViewCells还可以单击,单击时,它会展开 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { self.selectedIndex = indexPath self.didExpandCell() } func tableView(_ tabl
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.selectedIndex = indexPath
self.didExpandCell()
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if isExpanded && self.selectedIndex == indexPath{
return 300
}
return 126
在我的tableviewcell中,我有一个白色的自定义视图,这就是允许卡片视图外观的原因,它也是圆形的。未单击单元格时,该单元格为标准高度,如下所示:
单击单元格时,它会展开,如下所示:
正如您所看到的,当单元格未单击时,底部部分是直的而不是圆的,我尝试使用此代码使其圆化,但它不起作用
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cells", for: indexPath) as! ProfileTableViewCell
if TableView.rowHeight == 126 {
cell.CustomView = UIView(frame: CGRect(x: 7, y: 8, width: 359, height: 20))
return cell
}
else{
}
}
这个问题可以通过在视图中使用测试标签来解决,该视图在单元格展开时使视图变圆而不是单元格从视图中移除半径。 或者您可以设置特定的角半径,例如左上角、右上角、左下角、右下角
Use below example to set the corner radius
let rectShape = CAShapeLayer()
rectShape.bounds = self.myView.frame
rectShape.position = self.myView.center
rectShape.path = UIBezierPath(roundedRect: self.myView.bounds, byRoundingCorners: [.bottomLeft , .bottomRight , .topLeft], cornerRadii: CGSize(width: 20, height: 20)).cgPath
self.myView.layer.backgroundColor = UIColor.green.cgColor
self.myView.layer.mask = rectShape
您的自定义视图始终是完全展开的高度,因为您从来没有做过任何事情,至少按照您共享的代码进行设置 尝试的修复不起作用的原因是,如果未使用委托方法确定单元格高度,并且正在使用委托方法,则TableView.rowHeight是用于单元格的高度。TableView.rowHeight将始终是您最初设置的值,并且不会因单元格而异,因此如果将其设置为126,它将永远不会匹配或始终匹配
如果您想获得想要的效果,我建议您查看自动布局或自动调整大小的遮罩,使自定义视图的大小与单元格的大小一样动态。我解决了此问题:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cells", for: indexPath) as! ProfileTableViewCell
if isExpanded && self.selectedIndex == indexPath {
cell.CustomView.frame = CGRect(x: 7, y: 8, width: 359, height: 283)
}
else {
cell.CustomView.frame = CGRect(x: 7, y: 8, width: 359, height: 116)
}
}