首页 > 解决方案 > 使用 UINib 加快表格视图单元格的加载速度

问题描述

我正在开发 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;
}

标签: androidiosiphonexmppchat

解决方案


推荐阅读