UITableView delete button not appearing

江枫思渺然 提交于 2019-11-29 16:35:14
digthewells

I had to work around it by using a different mechanism. I did a test and when I used a UITableViewController it worked fine. When I added a UITableView to a UIViewController, implementing the same thing as the UITableViewController does, it does not work. I do not know what I missed, but using the UIViewController as opposed to the UITableViewController caused the delete button to not appear.

Mike Vosseller

I ran into the same issue. The problem was that my tableView's frame was not being properly resized inside the popover. In reality the delete button was being displayed but it was outside the bounds of the popover so it couldn't be seen.

I fixed this by ensuring the tableView resizes appropriately. For me this meant setting the tableView's autoresizingMask like this:

self.tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

The reason others were able to fix this by switching to a UITableViewController is because its tableView is properly resized.

I know you fixed it using a UITableViewController but I couldn't in my case.

Looking around I just found the answer here, you need more plumbing to get the job done. Fortunately its pretty easy. Add this to the UIViewController containing a reference to your UITableView

// objc
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
    [super setEditing:editing animated:animated];
    [tableView setEditing:editing animated:animated];
}

// swift
override func setEditing(editing: Bool, animated: Bool) {
    super.setEditing(editing, animated: animated)
    self.tableView.setEditing(editing, animated: animated)
}

The problem is that you're adding the labels using addSubview: to the cell's content view, and as they're added as last, they hide the other parts of the cell. Push them into the background by calling:

[cell.contentView sendSubviewToBack:label];

Remember to set the style of the delete button

-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}

I had the same issue when using the UIViewController.editButtonItem in the button bar (which automatically changes the editing property of the view controller). As I was using a UIViewController with a UITableView, I needed to delegate the setEditing to the tableview, e.g.

override func setEditing(_ editing: Bool, animated: Bool) {
    super.setEditing(editing, animated: animated)
    tableView.setEditing(editing, animated: animated)
}

If you are using custom tableview not the TVController. You must set atleast three delegates

Swift 4.2

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
    if editingStyle == .insert {

    } else if editingStyle == .delete {

    }
}


func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle {
    return .delete
}

func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
    return true
}

And most important is to add 'tableView.isEditing = true' in the viewDidload :)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!