删除分组的tableview中的行

时间:2011-06-28 12:14:56

标签: iphone objective-c ios uitableview

有些事情是错的,但我不知道是什么。我无法删除一行。当我的tableview进入editmode红色时,所有行附近的减号都会出现。触摸它们使得在行上显示删除按钮,当它触摸它变为(深红色)并且没有任何反复发生。

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

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

1 个答案:

答案 0 :(得分:1)

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

只需在":"方法中移除editingStyle后的冒号(commitEditingStyle:)即可。这将解决问题。那条线应该是这样的,

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