Ios 无法在UITableView中删除单元格

Ios 无法在UITableView中删除单元格,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我使用的是Xcode 5.1.1 为iPhone创建了单视图应用程序。我在Xcode iOS模拟器中进行测试,在我尝试删除之前,一切正常。多次检查代码。应用程序内编辑->红色减号出现在每一行,然后我按下减号并出现红色删除按钮,不能按下 #import "ViewController.h" #import "AddViewController.h" @interface ViewController () @end @implementation ViewController @synthe

我使用的是Xcode 5.1.1

为iPhone创建了单视图应用程序。我在Xcode iOS模拟器中进行测试,在我尝试删除之前,一切正常。多次检查代码。应用程序内编辑->红色减号出现在每一行,然后我按下减号并出现红色删除按钮,不能按下

#import "ViewController.h"
#import "AddViewController.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize data;

- (void)viewDidLoad
{
    [super viewDidLoad];

    data = [[NSMutableArray alloc] initWithObjects: @"item1", @"item2", @"item3", nil];
    self.navigationItem.leftBarButtonItem = self.editButtonItem;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

#pragma mark - Table view data source


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


-(NSInteger) tableView: (UITableView *) tableView numberOfRowsInSection:(NSInteger)section {
    return [data count];
}


-(UITableViewCell *) tableView: (UITableView *) tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *cellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];

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

    [[cell textLabel] setText:[data objectAtIndex:[indexPath row]]];

    return cell;
}

// Called after 'Save' is tapped on the AddViewController
- (IBAction) unwindToTableViewController: (UIStoryboardSegue *) sender {

    AddViewController *addViewController = (AddViewController *) [sender sourceViewController];
    NSString *text = [[addViewController textField] text];

    if(![text length] == 0 && ![[text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length] == 0) {

        // add it to the tap of the data source
        [data insertObject:text atIndex:0];
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];

        // insert it into tableView
        [[self tableView] beginUpdates];
        [[self tableView] insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
        [[self tableView] endUpdates];
    }
}

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

- (void)tableView:(UITableView *)tableView comitEditingStyle: (UITableViewCellEditingStyle) editingStyle forRowAtIndexPath: (NSIndexPath *) indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [data removeObjectAtIndex:[indexPath row]];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }
}

@end

您可以处理删除,但似乎没有启用它:

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

听起来您在删除事件后没有重新加载数据

点击delete(删除)按钮,从数据源中删除数据后,调用

[self.tableView reloadData];
看看这是否有效。

您在方法名称中错误地将commit作为comit。不确定这是否是个问题,但这肯定是个问题。应该是

- (void)tableView:(UITableView *)tableView commitEditingStyle: (UITableViewCellEditingStyle) editingStyle forRowAtIndexPath: (NSIndexPath *) indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [data removeObjectAtIndex:[indexPath row]];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }
}

这与你如何设置你的自动调整大小掩码有关——这是iOS7上出现的一种奇怪的行为。尝试在viewDidLoad函数中调用此函数-[self view].autoresizingMask=UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleWidth,并为tableView调用此函数-[tableView setAutoresizingMask:UIViewAutoresizingFlexibleWidth]。我在一个应用程序中遇到了同样的问题,此问题已得到解决。

将您的代码添加到ViewController,对于每个硬编码项目,断点在代码中停止三次,但仍无法按delete按钮这是默认设置。此外,如果这还没有到位,OP甚至可以开始删除。您是否尝试过实现代理协议CaneditRowatineXpath并返回YES?请参见@sooper,默认情况下,所有行都是可编辑的。此外,如果行不可编辑,OP如何才能看到“删除”按钮?问题不在于为什么不更新。不能按下必须调用处理程序的按钮,但应按下该按钮。所以断点不会停止在-voidtableView:UITableView*tableView comitEditingStyle:UITableViewCellEditingStyle editingStyle for RowIndexPath:NSIndexPath*indexPath{…}cos按钮未按下时
- (void)tableView:(UITableView *)tableView commitEditingStyle: (UITableViewCellEditingStyle) editingStyle forRowAtIndexPath: (NSIndexPath *) indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [data removeObjectAtIndex:[indexPath row]];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }
[self.tableView reloadData];}