UITableView delete button not appearing

2019-07-08 10:54发布

When I press the edit button I get the round delete icon to the left of the item. When I press the delete icon in the cell it 'turns' but the delete button does not show up so my commitEditingStyle never gets called because I have no delete button to press.

Just for fun...I change the cell to Insert I get the plus icon...I press it and commitEditingStyle is called.

I do not understand why I am not getting the delete button.

I have a UIViewController that I am showing in a popover. I am adding a UITableView to like so...

audioTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 44, self.view.frame.size.width, 303)];
audioTable.delegate = self;
audioTable.dataSource = self;
[self.view addSubview:audioTable];

I am using a custom cell with two labels in it to display text.

Here is the custom cell initWithFrame...

primaryLabel = [[UILabel alloc]initWithFrame:CGRectMake(25 ,8, 275, 25)];
primaryLabel.font = [UIFont systemFontOfSize:14];

secondaryLabel = [[UILabel alloc]initWithFrame:CGRectMake(25 ,28, 275, 25)];
secondaryLabel.font = [UIFont systemFontOfSize:12];

[self.contentView addSubview:primaryLabel];
[self.contentView addSubview:secondaryLabel];

[self.contentView sendSubviewToBack:primaryLabel];
[self.contentView sendSubviewToBack:secondaryLabel];

I have a delete button in a toolbar in the view controller that is hooked up to the edit call. Here is what I am doing in the edit call which is getting called fine because I am getting the delete symbol in the cell...

if([self.audioTable isEditing]) {
    [button setTitle:@"Edit"];
    [super setEditing:NO animated:NO];
    [self.audioTable setEditing:NO animated:YES];
} else {
    [button setTitle:@"Done"];
    [super setEditing:YES animated:NO];
    [self.audioTable setEditing:YES animated:YES];
}

I have implemented the following...

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

-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    //i don't think i need to implement this really
    return YES;
}

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        //do delete stuff
    }
}

Like I said everything is working normally, button presses and all work...just no delete button.

7条回答
劳资没心,怎么记你
2楼-- · 2019-07-08 11:08

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

查看更多
啃猪蹄的小仙女
3楼-- · 2019-07-08 11:09

Remember to set the style of the delete button

-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}
查看更多
小情绪 Triste *
4楼-- · 2019-07-08 11:10

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)
}
查看更多
欢心
5楼-- · 2019-07-08 11:19

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.

查看更多
爷、活的狠高调
6楼-- · 2019-07-08 11:21

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.

查看更多
够拽才男人
7楼-- · 2019-07-08 11:27

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];
查看更多
登录 后发表回答