简体   繁体   中英

Problems with editing UITableView

I have a UITableView with editing capabilities. The problem is whenever I delete or rearrange, the cells don't rearrange and the only cell that gets deleted is the last on in the table. The weird thing is that this wasn't happening before but it does is. Here is the code I am using -

self.periodArray = [[NSMutableArray alloc] initWithObjects:@"Period 1", @"Period 2", @"Period 3", @"Period 4", @"Period 5", @"Period 6", nil];

    self.classTableView = [[UITableView alloc] initWithFrame: CGRectMake(0, 0, 320, 44*[self.periodArray count]+63) style:UITableViewStylePlain];
    self.classTableView.delegate = self;
    self.classTableView.dataSource = self;
    self.classTableView.scrollEnabled = NO;
    [self.classTableView setSeparatorInset:UIEdgeInsetsMake(0, 34, 0, 0)];
    [self.view addSubview:self.classTableView];

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger) section {

    return [self.periodArray count];
}

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

    self.cell = nil;
    static NSString *AutoCompleteRowIdentifier = @"AutoCompleteRowIdentifier";
    self.cell = [tableView dequeueReusableCellWithIdentifier:AutoCompleteRowIdentifier];
    if (self.cell == nil) {
        self.cell = [[SchoolCell alloc]
                initWithStyle:UITableViewCellStyleDefault reuseIdentifier:AutoCompleteRowIdentifier];
    }
    self.cell.selectionStyle = UITableViewCellSelectionStyleNone;


    NSString *rowString = [NSString stringWithFormat:@"%d.", (int)indexPath.row + 1];
    self.cell.rowNumber = rowString;
    self.cell.classText.delegate = self;
    self.cell.teacherText.delegate = self;
    self.cell.delegate = self;

    return self.cell;
}

#pragma mark UITableViewDelegate methods

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


}

-(void)showNext:(UIButton *)sender {

    if (self.navigationItem.rightBarButtonItem.tintColor == [UIColor redColor]) {
        SelectClubsViewController *selectClubsViewController = [[SelectClubsViewController alloc] init];
        [self.navigationController pushViewController:selectClubsViewController animated:YES];
    }
}

-(void)editSelected:(UIButton *)sender {

    self.editClassesButton.hidden = YES;
    self.editLabel.hidden = YES;

    self.addClassButton.hidden = NO;
    self.addLabel.hidden = NO;

    self.editDoneButton.hidden = NO;
    self.editDoneLabel.hidden = NO;

    [self.classTableView setEditing: YES animated: YES];

}

-(void)doneSelected:(UIButton *)sender {

    self.editClassesButton.hidden = NO;
    self.editLabel.hidden = NO;

    self.addClassButton.hidden = YES;
    self.addLabel.hidden = YES;

    self.editDoneButton.hidden = YES;
    self.editDoneLabel.hidden = YES;

    [self.classTableView setEditing: NO animated: YES];

}

-(void)addSelected:(UIButton *)sender {

    [self.periodArray addObject:@"Period 8"];
    [self.classTableView reloadData];
    self.classTableView.frame = CGRectMake(0, 0, 320, 44*[self.periodArray count]+63);

    self.theLine.frame = CGRectMake(0, (self.classTableView.frame.size.height), 320, .5);
    self.addClassButton.frame = CGRectMake(0, self.classTableView.frame.size.height, 320, 44);
    self.addLabel.frame = CGRectMake(10, (self.classTableView.frame.size.height + 5), 100, 30);
    self.editLabel.frame = CGRectMake(10, (self.classTableView.frame.size.height + 5), 100, 30);
    self.editClassesButton.frame = CGRectMake(0, self.classTableView.frame.size.height, 320, 44);
    self.editDoneButton.frame = CGRectMake(0, (self.classTableView.frame.size.height + self.addClassButton.frame.size.height)-2, 320, 44);
    self.editDoneLabel.frame = CGRectMake(10, (self.classTableView.frame.size.height + self.addClassButton.frame.size.height + 5), 150, 30);

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


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


- (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}


- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
    [self.classTableView reloadData];
}


- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{

    self.addClassButton.frame = CGRectMake(0, self.classTableView.frame.size.height - 2, 320, 44);
    self.addLabel.frame = CGRectMake(10, (self.classTableView.frame.size.height + 3), 100, 30);
    self.editDoneButton.frame = CGRectMake(0, (self.classTableView.frame.size.height + self.addClassButton.frame.size.height)- 2, 320, 44);
    self.editDoneLabel.frame = CGRectMake(10, (self.classTableView.frame.size.height + self.addClassButton.frame.size.height + 3), 150, 30);
    [self.classTableView reloadData];

}

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

    [self.periodArray removeObjectAtIndex:indexPath.row];
    //[tableView reloadData];
    self.classTableView.frame = CGRectMake(0, 0, 320, 44*[self.periodArray count]+63);

    self.theLine.frame = CGRectMake(0, (self.classTableView.frame.size.height), 320, .5);
    self.addClassButton.frame = CGRectMake(0, self.classTableView.frame.size.height, 320, 44);
    self.editLabel.frame = CGRectMake(10, (self.classTableView.frame.size.height + 5), 100, 30);
    self.addLabel.frame = CGRectMake(10, (self.classTableView.frame.size.height + 5), 100, 30);
    self.editClassesButton.frame = CGRectMake(0, self.classTableView.frame.size.height, 320, 44);
    self.editDoneButton.frame = CGRectMake(0, (self.classTableView.frame.size.height + self.addClassButton.frame.size.height)-2, 320, 44);
    self.editDoneLabel.frame = CGRectMake(10, (self.classTableView.frame.size.height + self.addClassButton.frame.size.height + 5), 150, 30);
}

To add a section:

-(void)addSelected:(UIButton *)sender {

    [self.periodArray addObject:@"Period 8"];
    [self.classTableView reloadData];
    self.classTableView.frame = CGRectMake(0, 0, 320, 44*[self.periodArray count]+63);

    self.theLine.frame = CGRectMake(0, (self.classTableView.frame.size.height), 320, .5);
    self.addClassButton.frame = CGRectMake(0, self.classTableView.frame.size.height, 320, 44);
    self.addLabel.frame = CGRectMake(10, (self.classTableView.frame.size.height + 5), 100, 30);
    self.editLabel.frame = CGRectMake(10, (self.classTableView.frame.size.height + 5), 100, 30);
    self.editClassesButton.frame = CGRectMake(0, self.classTableView.frame.size.height, 320, 44);
    self.editDoneButton.frame = CGRectMake(0, (self.classTableView.frame.size.height + self.addClassButton.frame.size.height)-2, 320, 44);
    self.editDoneLabel.frame = CGRectMake(10, (self.classTableView.frame.size.height + self.addClassButton.frame.size.height + 5), 150, 30);

}

Inside the cell:

    self.classText = [[UITextField alloc] init];
    self.classText.delegate = self;
    self.classText.placeholder = @"Class";
    self.classText.frame = CGRectMake(145, 6, 160, 30);
    self.classText.keyboardAppearance = UIKeyboardAppearanceDark;
    self.classText.font = [UIFont fontWithName:@"HelveticaNeue" size:17];
    [self.classText addTarget:self action:@selector(textViewDidChange:) forControlEvents:UIControlEventEditingChanged];
    [self.contentView addSubview:self.classText];

You need to both remove the item from the datasource (periodArray) and remove the row from the tableView:

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

    // Remove from the dataSource
    [self.periodArray removeObjectAtIndex: indexPath.row];

    // Remove from the tableView
    [self.classTableView deleteRowsAtIndexPaths: @[ indexPath ] withRowAnimation: UITableViewRowAnimationAutomatic];

    ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM