编辑UITableView时出现问题

时间:2014-02-21 00:45:17

标签: ios iphone objective-c uitableview

我有一个带编辑功能的UITableView。问题是每当我删除或重新排列时,单元格不会重新排列,唯一被删除的单元格是表格中的最后一个单元格。奇怪的是,这种情况以前没有发生,但事实确实如此。这是我正在使用的代码 -

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);
}

添加部分:

-(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);

}

在细胞内:

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

1 个答案:

答案 0 :(得分:0)

您需要从数据源(periodArray)中删除该项并从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];

    ...
}