iOS-延迟将行添加到UITableView的最佳方式



我正在尝试制作一款与游戏《生命线》非常相似的iOS

游戏看起来就像是一个延迟后插入了一行的表格视图。

我已经设法插入了像这样的行

 for obj in array {
                    self.dialogue.append(obj)
                    self.tableView.beginUpdates()
                    self.tableView.insertRowsAtIndexPaths([NSIndexPath(forRow: 0, inSection: 0)], withRowAnimation: UITableViewRowAnimation.Fade)
                    self.tableView.endUpdates()
                    }}

*注意self.dialogue是我的表视图的数据源。

然而,我不知道如何在添加新行之间添加延迟,例如

->插入行->等待3秒->插入另一行

我怎么能像在游戏中那样做呢?

您可以实现单独的方法或块来插入行,并在for循环内部延迟调用它。例如

var dispatchTime: dispatch_time_t = dispatch_time(DISPATCH_TIME_NOW, Int64(1.0 * Double(NSEC_PER_SEC))) 
for obj in array {
  dispatch_after(dispatchTime, dispatch_get_main_queue(), { 
    self.dialogue.append(obj)
    self.tableView.beginUpdates()
    self.tableView.insertRowsAtIndexPaths([NSIndexPath(forRow: 0, inSection: 0)], withRowAnimation: UITableViewRowAnimation.Fade)
    self.tableView.endUpdates()
  })
}}

最新更新