我在UITableView
插入,删除行时遇到问题。
我试图做一个" Facebook信使"在UITableView
看到效果。我有许多单元格,每个单元格都指示一条消息,在它们之间,有许多单元格,每个单元格表示到目前为止已经看过此消息的人。所有这些数据都正确存储。
现在,我希望当我收到一条看到的通知,从表中删除一个看到的单元格,然后根据我的数据源(在大多数情况下是最后一项)在正确的索引处插入另一个单元格。当用户发送消息时,我将他的消息添加到表中然后删除他最后看到的单元格,然后在他的消息之后重新插入另一个单元格。
现在调试逻辑一切都没关系,更新数据源并请求在UITableView
上插入,删除,但我仍然得到:
由于未捕获的异常而终止应用 ' 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 :(得分:0)
调试它,可能是您在更新表视图时错过了更新数据源。