2017-09-21 4 views
0

각 단어 사이에 2 초의 일시 중지를 사용하여 각 단어를 표보기 셀에 표시하려고합니다. 이것이 가능한가? 나는 셀을 수정하고 다음과 같이 다시로드 다시로드 유지하고 싶지 않은 : 당신이 이것을 달성하기 위해 Timer를 사용할 수한 번에 tableviewcell에 한 단어 표시

var fullNameArr = message.characters.split{$0 == " "}.map(String.init) 
     var firstWord = true 
     for word in fullNameArr { 
      if firstWord { 
       firstWord = false 
      captionsArray.append(CaptionObject(isMacro:isMacro, number: numberToCall!.number, caption: word, time: String(describing:currentTimeInMiliseconds()))) 
       self.reloadTableAndScroll() 
      } else { 
       let cap = self.captionsArray.last! 
       cap.caption = cap.caption + " " + word 
       captionsArray.remove(at: captionsArray.count) 
       captionsArray.append(cap) 
       self.reloadTableAndScroll() 
      } 


      self.reloadTableAndScroll() 
     } 
+0

테이블로드가 완료된 후 다음 셀 텍스트를 표시하는 타이머를 2 초마다 실행합니다. 좀 더 미세한 세부 사항이 남아 있지만 어쩌면 이것이 당신을 위해 일할 수도 있습니다. – TNguyen

답변

0

.

는 클래스의 상단에 타이머 변수를 선언하는 Timer 만들고, viewDidLoad 방법에 초기화 :
var timer: Timer? 

override func viewDidLoad() { 
    super.viewDidLoad() 

    timer = Timer.scheduledTimer(timeInterval: 2.0, target: self, selector: #selector(addWordCell), userInfo: nil, repeats: true) 

    // ... 
} 

은 이제 매 2 초, 당신의 addWordCell 방법이

를 호출됩니다. 그건 그렇고, 항상 테이블 뷰를 다시로드하는 대신 insertsRows 메서드를 사용하는 것이 좋습니다. 더 효율적입니다.

var words = [String]() 

var currentWordIndex = 0 

let sentence = "Hello how are you doing today?" 

func addWordCell() { 

    let wordsArray = sentence.components(separatedBy: " ").map({ $0 }) 

    guard currentWordIndex < wordsArray.count else { 
     return 
    } 

    words.append(wordsArray[currentWordIndex]) 

    tableView.beginUpdates() 
    tableView.insertRows(at: [IndexPath(row: words.count-1, section: 0)], with: .fade) 
    tableView.endUpdates() 

    currentWordIndex += 1 
} 

을 그리고 당신은 물론 당신의 테이블 뷰 데이터 소스 방법을 변경해야 할 것입니다 : 예를 들어, 당신은이처럼 addWordCell 방법을 쓸 수 당신이 좋은 작은 페이딩을 추가하려면 이제

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 

    return words.count 
} 

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

    let cell = tableView.dequeueReusableCell(withIdentifier: reuseIdentifier, for: indexPath) 

    cell.textLabel?.text = words[indexPath.row] 

    return cell 
} 

을 그것 뿐이다

override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) { 

    cell.alpha = 0.0 

    UIView.animate(withDuration: 0.6, animations: { 

     cell.alpha = 1.0 
    }) 
} 

: 새로운 셀이 나타날 때마다 효과는, 당신은 willDisplayCell 방법을 사용할 수 있습니다! 분명히 코드를 더 향상시키고 필요에 맞게 사용자 정의 할 수는 있지만 최소한이 방법을 사용하면 가능한 접근법을 보여주는 약간의 작업 예제가 제공됩니다.