Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

修正了新消息插入跟下拉加载消息的时候出现崩溃的现象 #330

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,25 @@ - (void)exMainQueue:(void (^)())queue {
}

- (void)addMessage:(XHMessage *)addedMessage {
int64_t delayInSeconds = 0.2;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void) {
NSMutableArray *messages = [NSMutableArray arrayWithArray:self.messages];
[messages addObject:addedMessage];

NSMutableArray *indexPaths = [NSMutableArray arrayWithCapacity:1];
[indexPaths addObject:[NSIndexPath indexPathForRow:messages.count - 1 inSection:0]];
self.messages = messages;
// [weakSelf.messageTableView beginUpdates];
[self.messageTableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationNone];
// [weakSelf.messageTableView endUpdates];
[self.messageTableView reloadData];
[self scrollToBottomAnimated:YES];


});

/*
WEAKSELF
[self exChangeMessageDataSourceQueue:^{
NSMutableArray *messages = [NSMutableArray arrayWithArray:weakSelf.messages];
Expand All @@ -238,6 +257,7 @@ - (void)addMessage:(XHMessage *)addedMessage {
[weakSelf scrollToBottomAnimated:YES];
}];
}];
*/
}

- (void)removeMessageAtIndexPath:(NSIndexPath *)indexPath {
Expand All @@ -254,6 +274,44 @@ - (void)removeMessageAtIndexPath:(NSIndexPath *)indexPath {
static CGPoint delayOffset = {0.0};
// http://stackoverflow.com/a/11602040 Keep UITableView static when inserting rows at the top
- (void)insertOldMessages:(NSArray *)oldMessages completion:(void (^)())completion {
int64_t delayInSeconds = 0.2;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void) {

delayOffset = self.messageTableView.contentOffset;
NSMutableArray *indexPaths = [[NSMutableArray alloc] initWithCapacity:oldMessages.count];
NSMutableIndexSet *indexSets = [[NSMutableIndexSet alloc] init];
[oldMessages enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:idx inSection:0];
[indexPaths addObject:indexPath];

delayOffset.y += [self calculateCellHeightWithMessage:[oldMessages objectAtIndex:idx] atIndexPath:indexPath];
[indexSets addIndex:idx];
}];

NSMutableArray *messages = [[NSMutableArray alloc] initWithArray:self.messages];
[messages insertObjects:oldMessages atIndexes:indexSets];
[UIView setAnimationsEnabled:NO];
self.messageTableView.userInteractionEnabled = NO;
//[self.messageTableView beginUpdates];

self.messages = messages;

[self.messageTableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationNone];
// [weakSelf.messageTableView reloadData];

[UIView setAnimationsEnabled:YES];

[self.messageTableView setContentOffset:delayOffset animated:NO];
self.messageTableView.userInteractionEnabled = YES;
if (completion) {
completion();
}


});

/*
WEAKSELF
[self exChangeMessageDataSourceQueue:^{
delayOffset = weakSelf.messageTableView.contentOffset;
Expand Down Expand Up @@ -291,6 +349,7 @@ - (void)insertOldMessages:(NSArray *)oldMessages completion:(void (^)())completi
}
}];
}];
*/
}

- (void)insertOldMessages:(NSArray *)oldMessages {
Expand Down