如何在swift中添加一些延迟的新单元格到tableview?

时间:2016-08-04 11:52:29

标签: ios objective-c swift uitableview

我想添加一些延迟的新表视图单元格。我使用下面的块来创建延迟。

func delay(delay:Double, closure:()->()) {
        dispatch_after(
            dispatch_time(
                DISPATCH_TIME_NOW,
                Int64(delay * Double(NSEC_PER_SEC))
            ),
            dispatch_get_main_queue(), closure)
    }

它工作正常,但如果我切换到其他标签并在延迟期间回来。一些空的重复单元格正在添加。

我尝试使用以下代码添加新单元格

  delay(1){
                    NSLog("lesson card")
                    let lessonCard : LessonsLauncher = messageCard.object as! LessonsLauncher
                    self.messageCards.append(messageCard)
                    self.currentMessageCard = lessonCard
                    let lessonCardOptions : MessageCard = MessageCard()
                    lessonCardOptions.viewType = .LESSON_CARD_OPTIONS
                    self.messageCards.append(lessonCardOptions)
                    let chatMessage = lessonCard.toChatMessage()
                    ChatMessage.createChatMessage(chatMessage)
                    let indexpath1 = NSIndexPath(forRow: self.messageCards.count-2, inSection: 0)
                    let indexpath2 = NSIndexPath(forRow: self.messageCards.count-1, inSection: 0)
                    self.tableView.insertRowsAtIndexPaths([indexpath1,indexpath2], withRowAnimation: .Bottom)
                    self.tableView.scrollToRowAtIndexPath(indexpath2, atScrollPosition: UITableViewScrollPosition.Bottom, animated: true)
                   // self.nextIndex = lessonCard.next
                }

0 个答案:

没有答案