iOS应用程序 - 删除表格视图处于编辑模式时未显示的按钮

时间:2013-10-28 15:21:31

标签: ios objective-c

我对iOS很新,但是我创建了一个带有表视图的视图控制器,我希望将其置于编辑模式。这是我的视图控制器实现文件代码:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize progsTable, programmes;

- (void)viewDidLoad
{
    [super viewDidLoad];
    programmes = [[NSMutableArray alloc] initWithObjects:@"one",@"two",@"three",nil];
    //set the title
    self.title = @"Programmes";

    //add an edit button
    self.navigationItem.leftBarButtonItem = self.editButtonItem;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    [progsTable setEditing:editing animated:animated];
}

#pragma mark - UITableView Datasource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return programmes.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if(cell==nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }

    cell.textLabel.text = [programmes objectAtIndex: indexPath.row];

    return cell;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *) indexPath {
    return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle==UITableViewCellEditingStyleDelete) {
        //remove from array
        [programmes removeObjectAtIndex:indexPath.row];

        //remove from table
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }
}
#pragma mark - UITableView Delegate methods

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

}

@end

我在左上角看到了编辑按钮但是当我点击它时,红色删除按钮没有出现 - 我做错了什么?

4 个答案:

答案 0 :(得分:2)

我认为您需要添加以下代码:

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

这告诉UITableView你想要哪种编辑方式。

答案 1 :(得分:2)

运行您的确切代码确实有效 - 我在测试时会显示删除按钮。

我认为问题是你没有将名为progsTable的@property分配给你的表视图。

要解决此问题,请执行以下操作:

  • 转到您的故事板
  • 右键单击View Controller
  • 将鼠标拖到表视图时,单击并按住名为progsTable的插座旁边的+,如下所示:

enter image description here

尝试运行您的应用 - 现在应该出现删除按钮。

答案 2 :(得分:1)

使用此

- (IBAction)deleteDrug:(id)sender event:(id)event {
    selectedButtonIndex = [self indexPathForButton:sender event:event];

    [tableView setEditing:YES animated:YES];
}


- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath == selectedButtonIndex) {
        return YES;
    }

    return NO;
}

您可能需要为数据源实现此方法。

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

答案 3 :(得分:0)

检查您是否还在界面构建器中添加了自动布局约束,以便单元格的最后一行与设备的宽度对齐。

在模拟器中运行ipad上的代码,如果删除按钮显示在那里但不在iphone模拟器上,则可能只是内容未正确对齐到设备的视口。

我的删除'当我滑动时,按钮没有显示但是单元格标题内容向左移动,但是当我将右边缘约束设置为' 0时,我没有在我的故事板中设置我的单元格宽度的约束。 #39;或者' 16',出现删除按钮。

相关问题