我剛剛創建了一個 tableView,我想實作一個允許用戶洗掉行的函式。為此,我實作了這個功能:
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
// Zeile l?schen (Z.b. dann aus dem Array oder aus dem dauerhaftem CoreData)
tableView.deleteRows(at: [indexPath], with: .fade)
}
}
但是對于我的問題,您仍然必須知道我使用以下代碼配置了我的單元:
let cell = tableView.dequeueReusableCell(withIdentifier: "timerCell", for: indexPath) as! timerTableViewCell
cell.layer.borderWidth = 1
// cell.layer.borderColor = CGColor(red: 41 / 255, green: 171 / 255, blue: 226 / 255, alpha: 1)
cell.layer.borderColor = CGColor(red: 0, green: 0, blue: 0, alpha: 1)
cell.textLabel?.text = "Timer"
cell.detailTextLabel!.text = "12:40"
cell.layer.cornerRadius = 18
cell.clipsToBounds = true
cell.layoutMargins.left = 30
cell.layoutMargins.right = 30
return cell
但是現在我在洗掉單元格和單元格框架時遇到問題:我的單元格有一個圓形邊框,如果我現在想在我的應用程式中洗掉這個單元格,邊框突然變成方形并且即使在洗掉之后也不會再次變成圓形洗掉被取消。不幸的是,這看起來很糟糕。以下是螢屏截圖:
1. 2.
3.
有沒有人遇到過這個問題或想法并且可以幫助我?
PS:我希望它看起來像這樣:
uj5u.com熱心網友回復:
這意味著CALayer正在重建單元格(可能是在調整單元格大小以顯示洗掉按鈕時)。
您可能想要創建一個自定義視圖單元格類并覆寫func layoutSublayers(of: CALayer)單元格的(它通過 UIView 和 CALayerDelegate 繼承)。在該方法內部,您可以呼叫 super 然后在您的視圖層上設定角半徑。
uj5u.com熱心網友回復:
我現在嘗試了一些新的東西,只是簡單地在我的單元格上放置一個視圖,然后將這個視圖而不是直接舍入單元格。但是我總是收到一條帶有所需初始化的錯誤訊息。這是我的代碼:
let view: UIView = {
let view = UIView()
view.backgroundColor = .white
view.layer.borderWidth = 1.5
view.layer.borderColor = CGColor(red: 0, green: 0, blue: 0, alpha: 1)
view.layer.cornerRadius = 18
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: .default, reuseIdentifier: "timerCell")
super.awakeFromNib()
setConstrains()
}
func setConstrains() {
self.addSubview(view)
view.centerXAnchor.constraint(equalTo: self.centerXAnchor).isActive = true
view.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true
view.leftAnchor.constraint(equalTo: self.leftAnchor, constant: 0).isActive = true
view.rightAnchor.constraint(equalTo: self.rightAnchor, constant: 0).isActive = true
view.heightAnchor.constraint(equalTo: self.heightAnchor, constant: 0).isActive = true
view.bottomAnchor.constraint(equalTo: self.bottomAnchor, constant: 0).isActive = true
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}```
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/336450.html
