Sunday, 8 July 2018

Deleting a custom view from UITableView

I am swiping left to delete a cell that is a customview in Swift 3.
The cell is:
class CustomTableCell: SwipeTableViewCell
{
    public var atest = UILabel();
    public var btest = UILabel();

    var animator: Any?

    override init(style: UITableViewCellStyle, reuseIdentifier: String!)
    {
        super.init(style: style, reuseIdentifier: reuseIdentifier);

        let height = 140;

        atest = UILabel(frame: CGRect(x: 20 + (activityWidth / 2), y: 72, width: (activityWidth / 2) - 10, height: 30));

        btest = UILabel(frame: CGRect(x: 20 + (activityWidth / 2), y: 102, width: (activityWidth / 2) - 10, height: 30));

        self.contentView.addSubview(atest);
        self.contentView.addSubview(btest);
    }

Then in my table view controller I have:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
    let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier") as! CustomTableCell;

    cell.atest.text = "text from an array at indexpath.row";
    cell.btest.text = "another different text from an array";

    return cell;
}

The deletion in the table view controller happens here:
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath,
 for orientation: SwipeActionsOrientation) -> [SwipeAction]?
{
    let delete = SwipeAction(style: .destructive, title: "Delete")
    {
        action, indexPath in
        print("delete button tapped");

        // self.table.deleteRows(at: [indexPath], with: .none);

        // database is a string index array
        self.database.remove(at: indexPath.row);

        if (self.database.count == 0)
        {
            self.noText.isHidden = false;
            self.footer.isHidden = false;

            self.table.tableFooterView = self.footer;
        }

        // self.table.setNeedsDisplay();
    }

    delete.backgroundColor = UIColor.red;

    return [delete];

I do a delete by swiping left and it deletes everything correctly. The issue I have is that the deleting makes all the table view cells under the fold the same as the last visible cell.
How do I fix this? I am using a custom cell view too.


from Deleting a custom view from UITableView

No comments:

Post a Comment