2016-10-03 8 views
0

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

'setupTimersToFireAtDate'로 질문을 업데이트하십시오. 이 코드는 모두 메인 스레드에서 수행되기 때문에,'reloadRowsAtIndexPaths' 호출은'setupTimersToFireAtDate'가 완료 될 때까지 실행되지 않습니다. – rmaddy

+0

안녕하세요, rmaddy, 의견을 주셔서 감사합니다. 코드를 업데이트했지만 해당 메소드를 해당 파트로 제거했습니다. 당신이 말했듯이, 이들은 메인 스레드에 있고, 플레이어 타이밍은 매우 중요하지만 먼저 업데이트 할 행이 필요합니다. 사용자는 선택된 행 하이라이트를보기 위해 2 초를 기다리지 않아야합니다. – nontomatic

답변

1

에 대한 호출이 즉시 실행될 수 있도록 가장 쉬운 해결 방법은 setupTimersToFireDate으로 전화를 지연시키는 것입니다.

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
    // do some things 

    tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: .None) 

    dispatch_async(dispatch_get_main_queue(), { 
     setupTimersToFireAtDate() 
    }) 
} 
+0

나는 기꺼이 가장 쉬운 해결책을 취할 것이다! 그것은 큰 덕분에, rmaddy 많이 작동합니다. 나도 그 부분을 잘못 파견했다. – nontomatic