从UITableView中删除行并更新节标题

时间:2016-06-01 09:34:42

标签: ios objective-c uitableview header

我在UITableView中显示了一些行以及该部分标题中的行数,但是当我删除行时,视图看起来非常奇怪,将使用动画删除。如果我遗漏了某些内容,请查看我的代码。

- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[[UIView alloc]init] autorelease];

    view.backgroundColor = [UIColor colorWithRed:(249.0/255.0) green:(249.0/255.0) blue:(249.0/255.0) alpha:1];

    UILabel *label = [[[UILabel alloc]initWithFrame:CGRectMake(15, 15, 100, 15)] autorelease];
    [label setTextColor:[UIColor colorWithRed:0.56 green:0.56 blue:0.58 alpha:1.0]];
    [label setFont:[UIFont fontWithName:@"HelveticaNeue" size:12]];
    label.text = @"SELECTED ACCOUNTS (";

    NSString *numberOfAccounts = [NSString stringWithFormat:@"%li)", (unsigned long)[self->_selectedAccounts count]];

    if([self->_selectedAccounts count])
         label.text = [label.text stringByAppendingString:numberOfAccounts];

    [view addSubview:label];
    [view sizeToFit];
    [label sizeToFit];
    return view;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0)
    {
        NSMutableArray *selectedAcc = [[NSMutableArray alloc] initWithArray:self->_selectedAccounts];
        [selectedAcc removeObjectAtIndex:indexPath.row];
        self->_selectedAccounts = selectedAcc;
        [self->_MyTableView beginUpdates];
        [self->_MyTableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];
        [self->_MyTableView reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationNone];
        [self->_MyTableView endUpdates];
    }
}

3 个答案:

答案 0 :(得分:0)

在if条件中尝试此操作:

[tableView beginUpdates];

[yourArray removeObjectAtIndex:myIndexPath.section];
[yourArrayAnswerCount removeObjectAtIndex:myIndexPath.section]; 
[tableView deleteSections:[NSIndexSet indexSetWithIndex:myIndexPath.section] withRowAnimation:UITableViewRowAnimationRight];


    [tableView endUpdates];

这也可以帮助你... https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/TableView_iPhone/ManageInsertDeleteRow/ManageInsertDeleteRow.html

答案 1 :(得分:0)

替换

[self->_MyTableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];
[self->_MyTableView reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationNone];

[self->_MyTableView reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationTop];

reloadSections:应该意味着rowUpdates。这应该足够了

答案 2 :(得分:0)

如果您想删除并添加带动画的行,请尝试以下方式: -

[tableView beginUpdates];
[tableView insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationFade];
[tableView deleteRowsAtIndexPaths:deleteIndexPaths withRowAnimation:UITableViewRowAnimationFade];
[tableView endUpdates];

deleteIndexPaths是要从表中删除的NSIndexPaths数组。

NSArray *deleteIndexPaths = [[NSArray alloc] initWithObjects:
        [NSIndexPath indexPathForRow:0 inSection:0],
        [NSIndexPath indexPathForRow:1 inSection:0],
        [NSIndexPath indexPathForRow:2 inSection:0],
        nil];

我会帮助你