UITableView删除button不出现

当我按编辑button,我得到的项目左侧的圆形删除图标。 当我按下单元格中的删除图标时,它会“变成”,但删除button不显示,所以我的commitEditingStyle永远不会被调用,因为我没有删除button。

只是为了好玩…我改变单元格插入我得到加号图标…我按下它,并调用commitEditingStyle。

我不明白为什么我没有得到删除button。

我有一个UIViewController,我在popup窗口中显示。 我正在添加一个UITableView像这样…

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

我正在使用其中有两个标签的自定义单元格来显示文本。

这是自定义单元格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]; 

我在视图控制器中的一个工具栏中有一个删除button,可以连接到编辑调用。 这就是我正在做的编辑调用,因为我得到单元格中的删除符号被调用罚款…

 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]; } 

我已经实施了以下…

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

就像我说的一切工作正常,按下button,所有的工作…只是没有删除button。

我遇到了同样的问题。 问题是我的tableView的框架没有正确resize的popover内。 实际上,正在显示删除button,但它在popup窗口的边界之外,所以不能被看到。

我通过确保tableView适当调整来解决这个问题。 对我来说,这意味着像这样设置tableView的autoresizingMask

 self.tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; 

其他人能够通过切换到UITableViewController来解决这个问题的原因是因为它的tableView被正确resize。

我必须通过使用不同的机制来解决它。 我做了一个testing,当我用UITableViewController工作正常。 当我添加一个UITableView到一个UIViewController ,实现与UITableViewController一样的事情,它不起作用。 我不知道我错过了什么,但使用UIViewController而不是UITableViewController导致删除button不出现。

我知道你使用UITableViewController解决这个问题,但是我不能解决这个问题。

环顾四周,我刚刚在这里find了答案,你需要更多的pipe道来完成工作。 幸运的是它很容易。 将其添加到包含对您的UITableView的引用的UIViewController

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

问题是你使用addSubview添加标签:添加到单元格的内容视图,并且当他们被添加为最后时,他们隐藏了单元格的其他部分。 通过拨打电话将其推入背景中:

 [cell.contentView sendSubviewToBack:label]; 

请记住设置删除button的样式

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