草庐IT

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

coder 2023-09-08 原文

我有 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 swift : How can i delay cell to appear in a tableview?的更多相关文章

随机推荐