插入UITableViewCell With Animation并滚动到它

时间:2018-04-06 11:39:01

标签: ios objective-c swift xcode uitableview

我的要求很简单,我只需要添加TableCell rightRowAnimation并滚动到它。只要我在tableView高度内,就会发生这种情况,因为我越过tableView的高度以进一步添加单元格,它会添加单元格,但在滚动时不显示动画。

附件是一个简单的项目,UITableView供参考,感谢您的帮助!!

Sample Project with a simple UItableView and Add Button

代码

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyCell1"];
    BOOL myBool = indexPath.row%2 == 0;
    if(myBool){
        cell.backgroundColor = [UIColor redColor];
    }else{
        cell.backgroundColor = [UIColor whiteColor];
    }
    cell.textLabel.text = [self.dataArray objectAtIndex:indexPath.row];
    return cell;
}


-(IBAction)addTapped:(id)sender {
    [self.dataArray addObject:@"New Object"];
//    [_tableView beginUpdates];
    [_tableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:self.dataArray.count-1 inSection:0]] withRowAnimation:UITableViewRowAnimationRight];
//    [_tableView endUpdates];

    [_tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:self.dataArray.count-1 inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:YES];
}

2 个答案:

答案 0 :(得分:0)

对动画使用波纹管方法。

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
 {
        cell.alpha = 0;
        cell.layer.transform = CATransform3DMakeScale(0.5, 0.5, 0.5);
        [UIView animateWithDuration:0.7 animations:^{
            cell.alpha = 1;
            cell.layer.transform = CATransform3DScale(CATransform3DIdentity, 1, 1, 1);


           }];
}

改变您的要求

答案 1 :(得分:0)

你可以尝试这个来动画放在表格中的行。

-(void)viewWillAppear:(BOOL)animated
{
    dispatch_async(dispatch_get_main_queue(), ^{
        [UIView transitionWithView:yourtablename
                          duration:5.1f
                           options:UIViewAnimationOptionTransitionFlipFromTop
                        animations:^(void) {
                            [yourtablename reloadData];
                        } completion:NULL];
    });
}

您也可以更改动画风格。