2017-08-11 86 views
0

我有一個問題UITableView插入,刪除行。斯威夫特的UITableView,插入,然後刪除,然後再插入

我試圖做一個「Facebook的使者」在UITableView看到效果。我有許多單元,每個單元都指示一條消息,而在它們之間,有許多單元指示目前爲止看過此消息的人。所有這些數據都正確存儲。

現在,我想,當我收到通知,看到從表中刪除可見單元格,然後根據我的數據源(在大多數情況下,最後一個項目)插入適當的索引另一個。當用戶發送消息時,我將他的消息添加到表中,然後刪除他上次看到的單元格,然後在他的消息後重新插入另一個單元格。

現在調試邏輯每一件事是更新數據源,並請求插入,刪除對UITableView OK,但我仍然得到:

終止應用程序由於未捕獲的異常 「NSInternalInconsistencyException」,究其原因: 「無效更新:無效 中的行數爲0節中包含的 現有段的行的更新(36)後的數字必須是更新(36)之前等於包含在該部分 行數,加或減去 從該部分插入或刪除的行數(插入1個, 0已刪除)並加上或減去移入或移出 該部分的行數(移入0,移出0)。

我試圖把調用這個邏輯beginUpdates();endUpdates();內。所有的插入,刪除被稱爲內main thread (UI)

任何幫助,將不勝感激,非常感謝。

編輯

這裏是我的代碼:

//// Insert a seen status for user, handles seen logic 
private func insertSeen(event: UEvent?) -> (removed: Int, inserted: Int) { 
    // Make a holder for insertion result 
    var result: (removed: Int, inserted: Int) = (removed: -1, inserted: -1); 

    // Check if no event was given 
    if(event == nil){ 
     return result; } 

    if(event?.creatorID == "" && self.type == .single && self.participants.count > 0){ 
     event?.creatorID = self.participants[0].ID 
     event?.creatorName = self.participants[0].username; 
    } 

    // Get last seen for targeted user 
    let lastSeen = lastSeenForUser(id: (event?.creatorID)!); 
    // Get targeted participant 
    var particiapnt = self.participant(id: event!.creatorID); 

    // If sender is self -> he will not be found in particiapnts list 
    // So, we should get it's data from UserData 
    if(event?.creatorID == UserData.current.ID){ 
     particiapnt = UserData.current; 
    } 
    // Check if no event was given 
    if(particiapnt == nil){ 
     return result; } 

    let name = particiapnt?.username; 
    var image = particiapnt?.photo; 
    if(image != nil) { image = image?.Scale(Width: 20); } 

    // Store number of events before updates -> usefull to tell if seen has created a new event 
    let count: Int = self.events.count; 

    // Check if user has just sent a seen status 
    if((count > 0 && lastSeen == count - 1) || particiapnt == nil){ 
     // Return a no insertion result [-1,-1] 
     return result; 
    } 

    if(delegate != nil){ 
     delegate.beginUpdates(); 
    } 

    // Check if user hasn't sent any seen event before 
    if(lastSeen < 0){ 
     // Check if last event in group is seen 
     if(self.events.count > 0 && self.events.last?.type == .seen) { 
      // Add participant to last event 
      let _ = self.events.last?.addParticipant(id: (event?.creatorID)!, name: name!, image: image); 

      // Return where this user seen event was added 
      result = (removed: lastSeen, inserted: self.events.count - 1); 

      // Check delegate isn't null -> can apply changes 
      if(delegate != nil){ 
       // Tell delegate about the update 
       self.delegate.updateEvent(at: result.inserted, event: self.events[result.inserted]); 
      } 
     } else { 
      // If last event is'nt seen -> Create a new one 
      // Get a basic event 
      let seen = BasicEvent; 
      // Set event type as seen 
      seen?.type = .seen; 
      // Get where this participant was added 
      let _ = seen?.addParticipant(id: (event?.creatorID)!, name: name!, image: image); 
      // Insert the new craeted seen event to events 
      self.events.append(seen!); 

      // Return where this user seen event was added 
      result = (removed: lastSeen, inserted: self.events.count - 1); 

      // Check delegate isn't null -> can apply changes 
      if(delegate != nil){ 
       // Tell delegate about the update 
       self.delegate.addEvent(at: result.inserted, event: event, animation: .fade); 
      } 
     } 
    } 
    // Handle user has sent a seen event before 
    else { 
     // Get where the targeted user has sent his last seen status 
     let seen = self.events[lastSeen]; 
     // Remove targeted user from his last seen status event 
     let _ = seen.removeParticipant(id: (event?.creatorID)!); 

     // If seen is'nt empty yet -> jus t remov current participant 
     if(seen.participants.count > 0){ 
      // Return where this user seen event was added 
      result = (removed: lastSeen, inserted: self.events.count - 1); 

      // Check delegate isn't null -> can apply changes 
      if(delegate != nil){ 
       // Tell delegate about the update 
       self.delegate.updateEvent(at: result.removed, event: self.events[result.inserted]); 
      } 
     } else { 
      // Delete empty seen event 
      self.events.remove(at: lastSeen); 

      // Return where this user seen event was added 
      result = (removed: lastSeen, inserted: self.events.count - 1); 

      // Check delegate isn't null -> can apply changes 
      if(delegate != nil){ 
       print("coount: \(delegate.getTableView().numberOfRows(inSection: 0)) | 2 "); 

       // Tell delegate about the update 
       self.delegate.deleteEvent(at: result.removed, event: self.events[result.removed]); 
      } 
     } 

     // Check if last event in group is seen 
     if(self.events.count > 0 && self.events.last?.type == .seen) { 
      // Add participant to last event 
      let _ = self.events.last?.addParticipant(id: (event?.creatorID)!, name: name!, image: image); 

      // Return where this user seen event was added 
      result = (removed: lastSeen, inserted: self.events.count - 1); 

      // Check delegate isn't null -> can apply changes 
      if(delegate != nil){ 
       // Tell delegate about the update 
       self.delegate.updateEvent(at: result.inserted, event: self.events[result.inserted]); 
      } 

     } else { 
      // Get a basic event 
      let seen = BasicEvent; 
      // Set event type as seen 
      seen?.type = .seen; 
      // Get where this participant was added 
      let _ = seen?.addParticipant(id: (event?.creatorID)!, name: name!, image: image); 
      // Insert the new craeted seen event to events 
      self.events.append(seen!); 

      // Return where this user seen event was added 
      result = (removed: lastSeen, inserted: self.events.count - 1) 

      // Check delegate isn't null -> can apply changes 
      if(delegate != nil){ 
       // Tell delegate about the update 
       self.delegate.addEvent(at: result.inserted, event: seen, animation: .fade); 
      } 
     } 
    } 

    if(delegate != nil){ 
     delegate.endUpdates(); 
    } 

    // Return final result of this insertion 
    return result; 
} 
+0

你刪除你的應用程序的任何行?在重新加載表格之前,可能您已經刪除或刪除了該行。發佈你的代碼..有人可以清楚地指出錯誤 – cole

+0

@cole我已經發布了我的代碼,我不認爲我已經刪除或插入其他地方。 –

回答

0

調試numberOfRowsInSection方法,它仍然返回36,即使你在UITable插入1行

+0

謝謝!我已經注意到了,但仍然無法弄清楚爲什麼會發生這種行爲。 –

0

調試它,可能是你錯過了更新你的數據源,同時更新表格視圖。

+0

謝謝你,我已經做了很多次了,每件事情都按預期工作,但是在正確的時間/順序上不調用UITableView的插入和刪除操作。 –