ios swift : How can i delay cell to appear in a tableview?

标签 ios swift uitableview

我有 uitableview 我想让单元格一个接一个地显示 那会在几秒后

我已经在 cellForRowAtIndexPath 方法中尝试了 sleep(2)dispatchafter 但都不起作用

我只希望返回的单元格等待秒数。 这是代码:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell")! as! CustomCell

    cell.chatText.text = self.texts[indexPath.row]
    cell.textLabel?.textColor = UIColor.whiteColor()
    cell.backgroundColor = UIColor.clearColor()
    sleep(4)
    return cell
}

有什么想法吗?

最佳答案

cellForRow 方法中添加此代码:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell")! as! CustomCell

    cell.chatText.text = self.texts[indexPath.row]
    cell.textLabel?.textColor = UIColor.whiteColor()
    cell.backgroundColor = UIColor.clearColor()

    let delay = 0.55 + Double(indexPath.row) * 0.5; //calculate delay
    print(delay) // print for sure

    UIView.animateWithDuration(0.2, delay: delay, options: .CurveEaseIn, animations: {
        cell.alpha = 1.0 // do animation after delay
    }, completion: nil)

    return cell
}

你必须为每个单元格设置越来越多的延迟才能看到动画并一个接一个地显示

希望对你有帮助

关于ios swift : How can i delay cell to appear in a tableview?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36898316/

相关文章:

ios - 减少条形图和 x 轴之间的空间

ios - DispatchQueue.main.async如何存储它的 block

ios - 从父 ViewController 更改嵌入式 UITableViewController 数据

ios - 更改方向更改背景失败

iOS:未使用重用标识符清除单元格内容

ios - 无法将 NSString 从 kABBirthdayProperty 转换为 NSDate

swift - 尝试覆盖 Xcode 6.3 Beta 3 中的初始值设定项时出现构建错误

ios - 当用户滚动条形图时,我可以调用 SwiftCharts 中的委托(delegate)吗?

ios - 初学者 : How to present UITableView programmatically from another view

iphone - 在 iPhone 中延迟加载图像的最佳方法是什么?