在Swift中逐行淡入UITableViewCell [英] Fade in UITableViewCell row by row in Swift

查看:102
本文介绍了在Swift中逐行淡入UITableViewCell的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是swift的新手,我正在尝试使用UITableView,并且将逐个动画显示单元格。我怎样才能做到这一点?此外,如果新出现的单元格行不在屏幕上(隐藏在表格下方)。每个单元格出现时如何移动表格?

I am new to swift, I am trying to have a UITableView and the cells will be animated to appear one by one. How can I do that? Also, if the newly appeared row of cell not on the screen (hiding below the table). How can I move the table up when each cell appear?

    var tableData1: [String] = ["1", "2", "3", "4", "5", "6", "7"]

     override func viewWillAppear(animated: Bool) {
           tableView.scrollEnabled=false
    tableView.alpha=0.0
            NSTimer.scheduledTimerWithTimeInterval(NSTimeInterval(3), target: self, selector: "animateTable", userInfo: nil, repeats: false)
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.tableData1.count
    }

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell     {

        let cell:TblCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as! TblCell
        cell.lblCarName.textAlignment = NSTextAlignment.Justified;
return cell
}

func animateTable() {

//what should be the code?//
}


推荐答案

在UITableView中,行会在你获得时自动为你准备在你的范围视野中。我假设你至少不是最初能够滚动tableView,所以我们会以编程方式滚动它,使行显示出来。我们怎么做?实际上UITableView有一个让我们将它滚动到特定行的方法:

In the UITableView, the rows are prepared automatically for you when they get to be in your "range vision". I'm assuming you are, at least not initially, being able to scroll the tableView, so we would scroll it programmatically making the rows appear as it goes. How are we doing that? Actually UITableView has a method that let us scroll it to a specific row:

scrollToRowAtIndexPath(indexPath : NSIndexPath, atScrollPosition : UITableViewScrollPosition, animated : Bool);

我太累了,无法编写代码,所以我将尝试解释。首先,对于每个单元格,只要加载(cellForRowAtIndexPath),就将alpha设置为0。
然后让我们假设我们的屏幕适合5行。你将按顺序动画他们的alphas(使用UIView.animateWithDuration)直到第五个(索引4),然后你将使用5,然后6,...直到其余部分(使用scrollPosition =)传递NSIndexPath的scrollToRowAtIndexPath。底部)。对于他们每个人,你会在他们加载后立即动画。请记住在这些互动之间留出一些时间。 (首先动画,将NSTimer运行到第二个,然后继续)。布尔动画当然应该是真的。

I'm too tired to write the code, so I'm going to try to explain. First, for every cell you set alpha to 0, as soon as they get loaded (cellForRowAtIndexPath). Then let's suppose our screen fits the 5 first rows. You are going to animate their alphas sequentially (using UIView.animateWithDuration ) until the fifth one (index 4), then you are going to scrollToRowAtIndexPath passing NSIndexPath using 5, then 6,... until the rest of them (using scrollPosition = .Bottom). And for each of them, you would animate as soon as they get loaded. Just remember to put some time between this interactions. (animate first, run NSTimer to the second, and it goes on). And the boolean animated should be true of course.

这篇关于在Swift中逐行淡入UITableViewCell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆