UITableView加载网络数据的优化

简介:

UITableView加载网络数据的优化

 

效果

 

源码

https://github.com/YouXianMing/Animations



//
//  TableViewLoadDataController.m
//  Animations
//
//  Created by YouXianMing on 16/2/2.
//  Copyright © 2016年 YouXianMing. All rights reserved.
//

#import "TableViewLoadDataController.h"
#import "V_2_X_Networking.h"
#import "TableViewLoadDataRootModel.h"
#import "LoadUrlDataCell.h"
#import "CellDataAdapter.h"
#import "NSString+LabelWidthAndHeight.h"
#import "UIFont+Fonts.h"
#import "UIView+SetRect.h"
#import "MessageAlertView.h"
#import "GCD.h"

@interface TableViewLoadDataController () <UITableViewDelegate, UITableViewDataSource, NetworkingDelegate>

@property (nonatomic, strong) UITableView                         *tableView;
@property (nonatomic, strong) V_2_X_Networking                    *dataNetworking;
@property (nonatomic, strong) NSMutableArray <CellDataAdapter *>  *datasArray;

@property (nonatomic, strong) MessageAlertView                    *showLoadingView;

@end

@implementation TableViewLoadDataController

- (void)setup {
    
    [super setup];
    
    [self createDataSource];
    
    [self createTableView];
    
    [self startNetworking];
}

- (void)createDataSource {
    
    self.datasArray = [NSMutableArray array];
}

- (void)startNetworking {
    
    self.showLoadingView             = [[MessageAlertView alloc] init];
    self.showLoadingView.message     = @"loading...";
    self.showLoadingView.contentView = self.contentView;
    [self.showLoadingView show];
    
    self.dataNetworking = [V_2_X_Networking getMethodNetworkingWithUrlString:@"https://api.app.net/stream/0/posts/stream/global"
                                                           requestDictionary:nil
                                                             requestBodyType:[HttpBodyType type]
                                                            responseDataType:[JsonDataType type]];
    self.dataNetworking.delegate        = self;
    self.dataNetworking.timeoutInterval = @(15);
    [self.dataNetworking startRequest];
}

- (void)createTableView {
    
    self.tableView                = [[UITableView alloc] initWithFrame:self.contentView.bounds];
    self.tableView.delegate       = self;
    self.tableView.dataSource     = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerClass:[LoadUrlDataCell class] forCellReuseIdentifier:@"LoadUrlDataCell"];
    [self.contentView addSubview:self.tableView];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.datasArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    CellDataAdapter *adapter = self.datasArray[indexPath.row];
    
    CustomAdapterTypeCell *cell = [tableView dequeueReusableCellWithIdentifier:adapter.cellReuseIdentifier];
    cell.dataAdapter            = adapter;
    [cell loadContent];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    CellDataAdapter *adapter = self.datasArray[indexPath.row];
    
    return adapter.cellHeight;
}

#pragma mark - NetworkingDelegate

- (void)requestSucess:(Networking *)networking data:(id)data {
    
    [self.showLoadingView hide];
    TableViewLoadDataRootModel *rootModel = [[TableViewLoadDataRootModel alloc] initWithDictionary:data];
    
    if (rootModel.meta.code.integerValue == 200) {
        
        [GCDQueue executeInGlobalQueue:^{
        
            for (int i = 0; i < rootModel.data.count; i++) {
                
                DataModel *dataModel = rootModel.data[i];
                
                if (dataModel.user.infomation.text.length <= 0) {
                    
                    continue;
                }
                
                NSDictionary *fontInfo   = @{NSFontAttributeName: [UIFont HeitiSCWithFontSize:14.f]};
                CGFloat       height     = [dataModel.user.infomation.text heightWithStringAttribute:fontInfo fixedWidth:Width - 80];
                CGFloat       cellHeight = height <= 50 ? 10 + 50 + 10 : 10 + height + 10;
                CellDataAdapter *dataAdapter = [CellDataAdapter cellDataAdapterWithCellReuseIdentifier:@"LoadUrlDataCell" data:dataModel
                                                                                            cellHeight:cellHeight cellType:0];
                [self.datasArray addObject:dataAdapter];
            }
            
            [GCDQueue executeInMainQueue:^{
                
                NSMutableArray *indexPaths = [NSMutableArray array];
                for (int i = 0; i < self.datasArray.count; i++) {
                    
                    [indexPaths addObject:[NSIndexPath indexPathForItem:i inSection:0]];
                }
                
                [self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];
            }];
        }];
        
    } else {
    
        AbstractAlertView *alertView     = [[MessageAlertView alloc] init];
        alertView.message                = @"No data now.";
        alertView.contentView            = self.contentView;
        alertView.autoHiden              = YES;
        alertView.delayAutoHidenDuration = 2.f;
        [alertView show];
    }
}

- (void)requestFailed:(Networking *)networking error:(NSError *)error {
    
    [self.showLoadingView hide];
    AbstractAlertView *alertView     = [[MessageAlertView alloc] init];
    alertView.message                = @"Network error.";
    alertView.contentView            = self.contentView;
    alertView.autoHiden              = YES;
    alertView.delayAutoHidenDuration = 2.f;
    [alertView show];
}

@end

细节


目录
相关文章
|
9天前
|
机器学习/深度学习 PyTorch 算法框架/工具
目标检测实战(一):CIFAR10结合神经网络加载、训练、测试完整步骤
这篇文章介绍了如何使用PyTorch框架,结合CIFAR-10数据集,通过定义神经网络、损失函数和优化器,进行模型的训练和测试。
29 2
目标检测实战(一):CIFAR10结合神经网络加载、训练、测试完整步骤
|
22天前
|
监控 安全 网络安全
云计算与网络安全:保护数据的关键策略
【9月更文挑战第34天】在数字化时代,云计算已成为企业和个人存储、处理数据的优选方式。然而,随着云服务的普及,网络安全问题也日益凸显。本文将探讨云计算环境中的网络安全挑战,并提供一系列策略来加强信息安全。从基础的数据加密到复杂的访问控制机制,我们将一探究竟如何在享受云服务便利的同时,确保数据的安全性和隐私性不被侵犯。
50 10
|
27天前
|
存储 安全 网络安全
云计算与网络安全:守护数据,构筑未来
在当今的信息化时代,云计算已成为推动技术革新的重要力量。然而,随之而来的网络安全问题也日益凸显。本文从云服务、网络安全和信息安全等技术领域展开,探讨了云计算在为生活带来便捷的同时,如何通过技术创新和策略实施来确保网络环境的安全性和数据的保密性。
|
1月前
|
缓存 网络协议 CDN
在网页请求到显示的过程中,如何优化网络通信速度?
在网页请求到显示的过程中,如何优化网络通信速度?
182 59
|
3天前
|
运维 监控 安全
连锁药店网络优化策略:一站式融合方案提升竞争力
在数字化浪潮下,线上药店通过技术创新和线上线下融合,正重塑购药体验,提供24小时服务和医保结算便利。面对激烈竞争,连锁药店和中小药店纷纷通过优化网络架构、提升服务质量和加强合规管理来增强竞争力,实现高效、安全的数字化转型。
|
7天前
|
机器学习/深度学习 算法 数据挖掘
基于GWO灰狼优化的GroupCNN分组卷积网络时间序列预测算法matlab仿真
本项目展示了基于分组卷积神经网络(GroupCNN)和灰狼优化(GWO)的时间序列回归预测算法。算法运行效果良好,无水印展示。使用Matlab2022a开发,提供完整代码及详细中文注释。GroupCNN通过分组卷积减少计算成本,GWO则优化超参数,提高预测性能。项目包含操作步骤视频,方便用户快速上手。
|
8天前
|
机器学习/深度学习 算法 数据安全/隐私保护
基于WOA鲸鱼优化的GroupCNN分组卷积网络时间序列预测算法matlab仿真
本项目展示了一种基于WOA优化的GroupCNN分组卷积网络时间序列预测算法。使用Matlab2022a开发,提供无水印运行效果预览及核心代码(含中文注释)。算法通过WOA优化网络结构与超参数,结合分组卷积技术,有效提升预测精度与效率。分组卷积减少了计算成本,而WOA则模拟鲸鱼捕食行为进行优化,适用于多种连续优化问题。
|
19天前
|
机器学习/深度学习 算法 数据安全/隐私保护
基于GA遗传优化的GroupCNN分组卷积网络时间序列预测算法matlab仿真
该算法结合了遗传算法(GA)与分组卷积神经网络(GroupCNN),利用GA优化GroupCNN的网络结构和超参数,提升时间序列预测精度与效率。遗传算法通过模拟自然选择过程中的选择、交叉和变异操作寻找最优解;分组卷积则有效减少了计算成本和参数数量。本项目使用MATLAB2022A实现,并提供完整代码及视频教程。注意:展示图含水印,完整程序运行无水印。
|
11天前
|
监控 自动驾驶 5G
|
20天前
|
SQL 安全 测试技术
网络安全与信息安全:保护数据的艺术
【9月更文挑战第36天】在数字化时代,网络安全和信息安全已成为维护个人隐私和企业资产的基石。本文深入探讨了网络安全漏洞、加密技术以及安全意识的重要性,旨在为读者提供一份知识宝典,帮助他们在网络世界中航行而不触礁。我们将从网络安全的基本概念出发,逐步深入到复杂的加密算法,最后强调培养安全意识的必要性。无论你是IT专业人士还是日常互联网用户,这篇文章都将为你打开一扇了解和实践网络安全的大门。
29 2

热门文章

最新文章