开发者

Delete a row in a grouped tableview

开发者 https://www.devze.com 2023-03-16 08:41 出处:网络
Well something is wrong but i dont know what. I cannot delete a row . when my tableview get in editmode red minus signs near all rows appear . touch on them make to appear delete button on row , whem

Well something is wrong but i dont know what. I cannot delete a row . when my tableview get in editmode red minus signs near all rows appear . touch on them make to appear delete button on row , whem i touch it it become (dark red) and nothing happens.

here is my code (not all) + i have no errors and warnings at compilation and runtime ...

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


Actually you have added an extra colon(":") in the commitEditingStyle: method name. Adding the extra colon has made this method a completely different method. So, your view controller thought that you haven't implemented the commitEditingStyle: method and didn't do anything while you edit your table view.

Just remove the colon(":") after editingStyle in the commitEditingStyle: method. That will fix the problem. That line should look like this,

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

精彩评论

暂无评论...
验证码 换一张
取 消