我在使用Swift上的表和单元格时遇到问题。
我创建了一个简单的UITableViewController,它有两个静态单元格。
回顾一下:
我要做的就是用JSON中的数据填充它们(到目前为止没问题)。
所有用户界面都是用故事板构建的,而非编码。
当我尝试改进布局UI时,问题就出现了。我想要做的是将这些单元格转换为圆角,更改背景颜色等。我能够实现这一点,正如您将在下面的代码中看到的那样。
我正在使用这段代码执行此操作:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath as IndexPath)
cell.backgroundColor = UIColor(colorLiteralRed: 1, green: 1, blue: 1, alpha: 0.5) // white color
cell.selectionStyle = .none
//Top Left Right Corners
let maskPathTop = UIBezierPath(roundedRect: cell.bounds, byRoundingCorners: [.topLeft, .topRight], cornerRadii: CGSize(width: 10.0, height: 10.0))
let shapeLayerTop = CAShapeLayer()
shapeLayerTop.frame = cell.bounds
shapeLayerTop.path = maskPathTop.cgPath
shapeLayerTop.fillColor = UIColor.white.cgColor
shapeLayerTop.strokeColor = UIColor.white.cgColor
//Bottom Left Right Corners
let maskPathBottom = UIBezierPath(roundedRect: cell.bounds, byRoundingCorners: [.bottomLeft, .bottomRight], cornerRadii: CGSize(width: 10.0, height: 10.0))
let shapeLayerBottom = CAShapeLayer()
shapeLayerBottom.frame = cell.bounds
shapeLayerBottom.path = maskPathBottom.cgPath
shapeLayerBottom.fillColor = UIColor.white.cgColor
shapeLayerBottom.strokeColor = UIColor.white.cgColor
//All Corners
let maskPathAll = UIBezierPath(roundedRect: cell.bounds, byRoundingCorners: [.topLeft, .topRight, .bottomRight, .bottomLeft], cornerRadii: CGSize(width: 10.0, height: 10.0))
let shapeLayerAll = CAShapeLayer()
shapeLayerAll.frame = cell.bounds
shapeLayerAll.path = maskPathAll.cgPath
shapeLayerAll.fillColor = UIColor.white.cgColor
shapeLayerAll.strokeColor = UIColor.white.cgColor
if (indexPath.row == 0 && indexPath.row == tableView.numberOfRows(inSection: indexPath.section)-1) {
cell.layer.mask = shapeLayerAll
} else if (indexPath.row == 0) {
cell.layer.mask = shapeLayerTop
} else if (indexPath.row == tableView.numberOfRows(inSection: indexPath.section)-1) {
cell.layer.mask = shapeLayerBottom
}
return cell
}
问题开始后,代码运行,我的标签消失,只保留改进的布局,颜色,单元格圆角等等。
我无法在任何地方找到的是保留这些标签的方法,无论细胞的变化如何。
有没有办法实现这一目标?我确定我错过了一些与表相关的理论。
我希望有人可以帮助我
详细信息:
答案 0 :(得分:0)
基于@AndréSlotta回复我的问题。这是我的最终代码:
override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
cell.backgroundColor = UIColor(colorLiteralRed: 1, green: 1, blue: 1, alpha: 0.5) // white color
cell.selectionStyle = .none
//Top Left Right Corners
let maskPathTop = UIBezierPath(roundedRect: cell.bounds, byRoundingCorners: [.topLeft, .topRight], cornerRadii: CGSize(width: 10.0, height: 10.0))
let shapeLayerTop = CAShapeLayer()
shapeLayerTop.frame = cell.bounds
shapeLayerTop.path = maskPathTop.cgPath
shapeLayerTop.fillColor = UIColor.white.cgColor
shapeLayerTop.strokeColor = UIColor.white.cgColor
//Bottom Left Right Corners
let maskPathBottom = UIBezierPath(roundedRect: cell.bounds, byRoundingCorners: [.bottomLeft, .bottomRight], cornerRadii: CGSize(width: 10.0, height: 10.0))
let shapeLayerBottom = CAShapeLayer()
shapeLayerBottom.frame = cell.bounds
shapeLayerBottom.path = maskPathBottom.cgPath
shapeLayerBottom.fillColor = UIColor.white.cgColor
shapeLayerBottom.strokeColor = UIColor.white.cgColor
//All Corners
let maskPathAll = UIBezierPath(roundedRect: cell.bounds, byRoundingCorners: [.topLeft, .topRight, .bottomRight, .bottomLeft], cornerRadii: CGSize(width: 10.0, height: 10.0))
let shapeLayerAll = CAShapeLayer()
shapeLayerAll.frame = cell.bounds
shapeLayerAll.path = maskPathAll.cgPath
shapeLayerAll.fillColor = UIColor.white.cgColor
shapeLayerAll.strokeColor = UIColor.white.cgColor
if (indexPath.row == 0 && indexPath.row == tableView.numberOfRows(inSection: indexPath.section)-1) {
cell.layer.mask = shapeLayerAll
} else if (indexPath.row == 0) {
cell.layer.mask = shapeLayerTop
} else if (indexPath.row == tableView.numberOfRows(inSection: indexPath.section)-1) {
cell.layer.mask = shapeLayerBottom
}
}
希望它可以帮助别人