From 38f90aa20adfa5fc346dda3cc151fa68bf2f6a5d Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Tue, 26 Mar 2019 10:27:45 +0300 Subject: Fix crashes --- Juick/ViewControllers/ChatViewController.m | 5 ++++- Juick/ViewControllers/MessagesViewController.m | 9 ++++++--- 2 files changed, 10 insertions(+), 4 deletions(-) (limited to 'Juick') diff --git a/Juick/ViewControllers/ChatViewController.m b/Juick/ViewControllers/ChatViewController.m index a76cc00..0de3d2a 100644 --- a/Juick/ViewControllers/ChatViewController.m +++ b/Juick/ViewControllers/ChatViewController.m @@ -47,7 +47,10 @@ [self.tableView beginUpdates]; [self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:YES]; [self.tableView endUpdates]; - [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:[self.messages count]-1 inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:NO]; + NSInteger messagesCount = [self.messages count]; + if (messagesCount > 0) { + [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:[self.messages count]-1 inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:NO]; + } } [self.refreshControl endRefreshing]; }]; diff --git a/Juick/ViewControllers/MessagesViewController.m b/Juick/ViewControllers/MessagesViewController.m index 5d47d01..f2a20a9 100644 --- a/Juick/ViewControllers/MessagesViewController.m +++ b/Juick/ViewControllers/MessagesViewController.m @@ -70,9 +70,12 @@ NSString* const messageCellIdentifier = @"messageCell"; [self.tableView endUpdates]; [self.refreshControl endRefreshing]; if (self.shouldScrollToUnreadOnRefresh) { - NSInteger itemToScroll = [self.firstUnread integerValue] ? : self.messages.count - 1; - NSIndexPath *lastRow = [NSIndexPath indexPathForRow:itemToScroll inSection:0]; - [self.tableView scrollToRowAtIndexPath:lastRow atScrollPosition:UITableViewScrollPositionBottom animated:NO]; + NSInteger messagesCount = [self.messages count]; + if (messagesCount > 0) { + NSInteger itemToScroll = [self.firstUnread integerValue] ? : self.messages.count - 1; + NSIndexPath *lastRow = [NSIndexPath indexPathForRow:MIN(itemToScroll, messagesCount) inSection:0]; + [self.tableView scrollToRowAtIndexPath:lastRow atScrollPosition:UITableViewScrollPositionBottom animated:NO]; + } self.firstUnread = nil; } }]; -- cgit v1.2.3