iphone - 删除分组 TableView 中的一行

标签 iphone objective-c ios uitableview

嗯,有些不对劲,但我不知道是什么。我无法删除一行。当我的 tableview 进入编辑模式时,所有行附近都会出现红色减号。触摸它们使行上出现删除按钮,当我触摸它时它变成(深红色)并且没有任何反应。

这是我的代码(不是全部)+我在编译和运行时没有错误和警告......

- (void)viewDidLoad {

    [super viewDidLoad];

    // initialize singleton.
    appDelegate = (ExchangedAppDelegate *)[[UIApplication sharedApplication]delegate];

    // get banks from settings.plist
    NSString *tempPath = [self getPathOfSettingsPlist];

    appDelegate.banks = [[NSArray alloc]initWithContentsOfFile:tempPath];

    navigationItem.rightBarButtonItem = self.editButtonItem;

    backButton = [[UIBarButtonItem alloc]initWithTitle:@"Back" style:UIBarButtonSystemItemCancel target:self action:@selector(closeSettingsView)];

    navigationItem.leftBarButtonItem = backButton;

}


- (void)setEditing:(BOOL)editing animated:(BOOL)animated {

    [super setEditing:editing animated:animated];

    [banksTableView setEditing:editing animated:animated];

    if (editing) {

        addButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addBankFromList)];

        navigationItem.leftBarButtonItem = addButton;

    } else {


        navigationItem.leftBarButtonItem = backButton;

        NSString *tempPath = [self getPathOfSettingsPlist];

        self.picker.hidden = YES;

        [appDelegate.banks writeToFile:tempPath atomically:YES];
    }

}



- (void)addBankFromList {

    // not interesting

}

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

// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return [appDelegate.banks count];
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"BanksTableCell";

    indexForPath = indexPath;

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {      

        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];

        cell.selectionStyle = UITableViewCellEditingStyleNone;
    }

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

    return cell;
}


// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle) editingStyle: forRowAtIndexPath:(NSIndexPath *)indexPath {

     if (editingStyle == UITableViewCellEditingStyleDelete) {

         // Delete the row from the data source
         [appDelegate.banks removeObjectAtIndex:indexPath.row];

         [banksTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationRight];

         [banksTableView reloadData];
     } 
} 

//// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
    NSString *temp = [appDelegate.banks objectAtIndex:fromIndexPath.row];
    [appDelegate.banks removeObjectAtIndex:fromIndexPath.row];
    [appDelegate.banks insertObject:temp atIndex:toIndexPath.row];
} 

最佳答案

实际上您在commitEditingStyle: 方法名称中添加了一个额外的冒号(":")。添加额外的冒号使该方法成为一种完全不同的方法。因此,您的 View Controller 认为您没有实现 commitEditingStyle: 方法,并且在您编辑 TableView 时没有执行任何操作。

只需删除 commitEditingStyle: 方法中 editingStyle 之后的冒号(":")。这将解决问题。该行应如下所示,

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle) editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

关于iphone - 删除分组 TableView 中的一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6506228/

相关文章:

iphone - iPad 和网站自动调整大小/缩放

iphone - 每天更新一次应用角标(Badge)

ios - 通过 iTunes(或其他?)访问 iOS 应用程序中的 .plist 文件

iOS CoreData : How to setup the core data "connection"? Stanford UIDocument vs. Apple 模板?

iphone - 不工作时计数器

ios - 如果json数据为零,如何在tableview中隐藏单元格

iphone - 在静态 UITableView 中添加新部分

ios - 无法在 Objective-C Pod 文件中找到 'SquareInAppPaymentsSDK' 的规范

ios - 如何在 mailcore2 中获取电子邮件正文

iphone - 在哪里可以找到自定义 UITabBarSystemItem 图标?