Home > front end >  Swift UITableViewCell Border becomes angular when deleted
Swift UITableViewCell Border becomes angular when deleted

Time:10-26

I just created a tableView and I want to implement a function which allows the user to delete a row. For this I have implemented this function:

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)
        }
    }

But you still have to know for my question that I configured my cell with this code:

        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

But now I have a problem with deleting the cells and the frame of my cell: My cell has a round border and if I now want to delete this cell in my app, the border suddenly becomes square and does not become again round even after the deletion is canceled. Unfortunately that looks very bad. Here are the screenshots:

1. 2.

3.

Has anyone ever had this problem or ideas and could help me?

PS: I would like it to look like this:

CodePudding user response:

That would imply that the cell's CALayer is being reconstructed (probably when the cell is resized to show the delete button).

You might want to create a custom view cell class and override the func layoutSublayers(of: CALayer) of the cell (which it inherits though UIView and CALayerDelegate). Inside of that method you can call super then set the corner radius on your view's layer.

CodePudding user response:

I have now tried something new and simply put a view on my cell and then round out this view instead of the cell directly. But I always get an error message with the required init. This is my code:

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")
    }```
  • Related