当在tableView中选择一个单元格时,我需要重新加载该行,并设置一些定时器以在某些日期触发AVAudioPlayers(此设置需要几秒钟,但计时器已开始按照预期开始)。
但是,重新加载表格视图行(有效地突出显示该行)会延迟一段时间。
在选择保留良好的UI体验时,我需要重新加载行作为第一件事。
我试图将重新加载部分放在主线程中,如下所示:UITableView reloadData() gets fired but delays
由于某种原因,这不起作用:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
// do some things
dispatch_async(dispatch_get_main_queue(), {
tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: .None)
})
setupTimersToFireAtDate()
}
func setupTimersToFireAtDate() {
let start = NSDate()
// These are Float as they will be calculated with other Float values
var accumulatedTime: Float = 0.0
var elapsedTime: Float = 0.0
for event in events {
var player = AVAudioPlayer()
accumulatedTime += event.time
do {
// the urls array keeps urls to the sounds. For this example, I've set the index to be random, since it doesn't really matter.
player = try AVAudioPlayer(contentsOfURL: urls[Int.random])
// Players need to be maintained in order to playback, each player is removed with the audioPlayerDidFinishPlaying AVAudioPlayerDelegate method.
players += [player]
} catch let error as NSError {
loadError = error
}
elapsedTime = Float(NSDate().timeIntervalSinceDate(start))
// Some other calculations in between here.
player.prepareToPlay()
player.delegate = self
player.currentTime = 0.0
player.playAtTime(player.deviceCurrentTime + (NSTimeInterval(accumulatedNoteTime - elapsedTime)))
}
}
我可能在这里遗漏了一些东西,因为我不知道为什么这不起作用。
非常感谢任何帮助!
答案 0 :(得分:1)
最简单的解决方案是将呼叫延迟到setupTimersToFireDate
,以便可以立即调用reloadRowsAtIndexPaths
。
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
// do some things
tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: .None)
dispatch_async(dispatch_get_main_queue(), {
setupTimersToFireAtDate()
})
}