Ios 滚动到tableview中的行,然后闪烁该行

Ios 滚动到tableview中的行,然后闪烁该行,ios,swift,uitableview,Ios,Swift,Uitableview,我想滚动到该行并通过更改背景色来闪烁该行。 我可以滚动到该行。我使用方法cellForRow(at:)获取单元格,以便以后修改和制作动画。但是单元格为零。我不明白为什么它是nil,因为我可以滚动到具有相同indepath的行 let indexPath = IndexPath(row: rowIndex, section: 0) self.tableView.scrollToRow(at: indexPath, at: .top, animated: true) let cell = self.

我想滚动到该行并通过更改背景色来闪烁该行。 我可以滚动到该行。我使用方法
cellForRow(at:)
获取单元格,以便以后修改和制作动画。但是
单元格
为零。我不明白为什么它是nil,因为我可以滚动到具有相同
indepath
的行

let indexPath = IndexPath(row: rowIndex, section: 0)
self.tableView.scrollToRow(at: indexPath, at: .top, animated: true)
let cell = self.tableView.cellForRow(at: indexPath) // return nil.
if let cell = cell { // nil
// animation here.
}

根据文档,
cellForRow(at:indepath)
返回:

表示表中某个单元格的对象,如果该单元格不可见或indexPath超出范围,则为nil

当您调用
cellForRow(At:indepath)
时,您的单元格还不可见,因为动画滚动尚未完成

要跟踪滚动动画完成,您必须实施
UITableViewDelegate
协议:

class YourVC : UIViewController, UITableViewDelegate {

    override func viewDidLoad() {
        // ... your code
        self.tableView.delegate = self
    }

    func scrollViewDidEndScrollingAnimation(_ scrollView: UIScrollView) {
        let cell = self.tableView.cellForRow(at: indexPathToAnimate) // returns your cell object
        if let cell = cell {
            // animation here.
        }
    }
}

如果单元格不可见,您将得到一个nil值。通常,单元格动画在tableview中执行。委托
将显示单元格

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath)
这是当单元格出现时从左侧设置单元格动画的代码。通过检查需要在什么位置执行动画,可以修改和执行所需的动画

目标-C

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    //1. Setup the CATransform3D structure
    CATransform3D rotation;
    rotation = CATransform3DMakeScale(0, 0, 0);


    //2. Define the initial state (Before the animation)
    cell.layer.shadowColor = [[UIColor blackColor]CGColor];

    cell.layer.transform = rotation;
    cell.layer.anchorPoint = CGPointMake(1, 0);


    //3. Define the final state (After the animation) and commit the animation
    [UIView beginAnimations:@"rotation" context:NULL];
    [UIView setAnimationDuration:0.7];
    cell.layer.transform = CATransform3DIdentity;
    [UIView commitAnimations];
}
Swift 3

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {

    //1. Setup the CATransform3D structure
    let rotation = CATransform3DMakeScale(0, 0, 0)

    //2. Define the initial state (Before the animation)
    cell.layer.shadowColor = UIColor.black.cgColor
    cell.layer.transform = rotation
    cell.layer.anchorPoint = CGPoint(x: 1, y: 0)

    //3. Define the final state (After the animation) and commit the animation
    UIView.beginAnimations("rotation", context: nil)
    UIView.setAnimationDuration(0.7)
    cell.layer.transform = CATransform3DIdentity
    UIView.commitAnimations()
}