使用UINib加速表视图单元格加载

时间:2018-05-19 16:48:35

标签: android ios iphone xmpp chat

我正在开发iPhone聊天应用程序。

运行应用程序后,当我点击任何聊天室时,打开聊天室时会有1~2秒的延迟。

此延迟仅在运行后第一次发生。

回到聊天室列表后,当我再次点击任何聊天室时,没有延迟。

当我看到whatsapp时,没有任何延迟,只要点击聊天室就立即打开聊天室。

现在,在viewWillAppear上,已从DB中获取当前聊天室消息,我调用了UITableView的reloadData。

任何人都知道为什么存在延迟?

或者任何人都知道完美的架构可以毫不拖延地打开聊天室吗?

请指教,谢谢。

ChatRoomsViewController类

- (void)viewDidLoad {
...
self.chat_board = [[ChatBoardViewController alloc] initWithNibName:@"ChatBoardViewController" bundle:nil];

UINib *nib = [UINib nibWithNibName:@"ChatBoardRightCell" bundle:nil];
[_chat_board.board_table registerNib:nib forCellReuseIdentifier:@"ChatBoardRightCell"];

nib = [UINib nibWithNibName:@"ChatBoardLeftCell" bundle:nil];
[_chat_board.board_table registerNib:nib forCellReuseIdentifier:@"ChatBoardLeftCell"];

...
}

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
...
dispatch_async(dispatch_get_main_queue(), ^{
                OrientationEnabledNavigation *navcon = (OrientationEnabledNavigation*)self.tabBarController.selectedViewController;
                [navcon pushViewController:_chat_board animated:isAnim];
            });
...
}

ChatBoardViewController类

- (void)viewWillAppear:(BOOL)animated {
...
    [time_sections removeAllObjects];

    _messageTotalCount = [[ChatStorage sharedStorage] getMessageCount:chat_id];

    if (self.isLoadAll) {
        self.showingCount = _messageTotalCount;
        [time_sections addObjectsFromArray: [[ChatStorage sharedStorage] getTimeGroups:chat_id count:_messageTotalCount]];
    }else{

        [time_sections addObjectsFromArray: [[ChatStorage sharedStorage] getTimeGroups:chat_id count:self.showingCount]];
    }

    [self reloadData];
...

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
...
NSString *identifier = is_owner ? @"ChatBoardRightCell" : @"ChatBoardLeftCell";

            ChatBoardCell * cell = [tableView dequeueReusableCellWithIdentifier:identifier];

NSString * parsed_msg = [record objectForKey:@"parsed_message"];
            NSDictionary * decoded = [[ChatStorage sharedStorage] decodeStringToDictionary:parsed_msg];

[cell setText:decoded editMode:_edit_mode];
...
return cell;
}

0 个答案:

没有答案