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 = @();
[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 == ) { [GCDQueue executeInGlobalQueue:^{ for (int i = ; i < rootModel.data.count; i++) { DataModel *dataModel = rootModel.data[i]; if (dataModel.user.infomation.text.length <= ) { continue;
} NSDictionary *fontInfo = @{NSFontAttributeName: [UIFont HeitiSCWithFontSize:.f]};
CGFloat height = [dataModel.user.infomation.text heightWithStringAttribute:fontInfo fixedWidth:Width - ];
CGFloat cellHeight = height <= ? + + : + height + ;
CellDataAdapter *dataAdapter = [CellDataAdapter cellDataAdapterWithCellReuseIdentifier:@"LoadUrlDataCell" data:dataModel
cellHeight:cellHeight cellType:];
[self.datasArray addObject:dataAdapter];
} [GCDQueue executeInMainQueue:^{ NSMutableArray *indexPaths = [NSMutableArray array];
for (int i = ; i < self.datasArray.count; i++) { [indexPaths addObject:[NSIndexPath indexPathForItem:i inSection:]];
} [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 = .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 = .f;
[alertView show];
} @end
细节
UITableView加载网络数据的优化的更多相关文章
- Android之ListView&Json加载网络数据
使用到的主要内容: 1.Json 解析网络数据 2.异步任务加载图片和数据 3.ListView 的内存空间优化(ConvertView)和运行时间优化(ViewHolder) 4.ListView ...
- android122 zhihuibeijing 新闻中心NewsCenterPager加载网络数据实现
新闻中心NewsCenterPager.java package com.itheima.zhbj52.base.impl; import java.util.ArrayList; import an ...
- iOS中 UIWebView加载网络数据 技术分享
直奔核心: #import "TechnologyDetailViewController.h" #define kScreenWidth [UIScreen mainScreen ...
- NSURLConnection 异步加载网络数据
#import "ViewController.h" @interface ViewController () @end @implementation ViewControlle ...
- 鸿蒙的fetch请求加载聚合数据的前期准备工作-手动配置网络权限
目录: 1.双击打开"config.json"文件 2.找到配置网络访问权限位置1 3.配置内容1 4.默认访问内容是空的 5.添加配置内容2 6.复制需要配置的网络二级URL 7 ...
- ios网络学习------4 UIWebView的加载本地数据的三种方式
ios网络学习------4 UIWebView的加载本地数据的三种方式 分类: IOS2014-06-27 12:56 959人阅读 评论(0) 收藏 举报 UIWebView是IOS内置的浏览器, ...
- DevExpress ChartControl大数据加载时有哪些性能优化方法
DevExpress ChartControl加载大数据量数据时的性能优化方法有哪些? 关于图表优化,可从以下几个方面解决: 1.关闭不需要的可视化的元素(如LineMarkers, Labels等) ...
- js防抖和节流优化浏览器滚动条滚动到最下面时加载更多数据
防抖和节流,主要是用来防止过于平凡的执行某个操作,如浏览器窗口变化执行某个操作,监听某个input输入框keyup变化,瀑布流布局时Y轴滚动,图片加载. js函数的防抖 经过一段事件才执行某个操作,如 ...
- Learning Spark中文版--第五章--加载保存数据(2)
SequenceFiles(序列文件) SequenceFile是Hadoop的一种由键值对小文件组成的流行的格式.SequenceFIle有同步标记,Spark可以寻找标记点,然后与记录边界重新 ...
随机推荐
- 【论文阅读】Learning Spatial Regularization with Image-level Supervisions for Multi-label Image Classification
转载请注明出处:https://www.cnblogs.com/White-xzx/ 原文地址:https://arxiv.org/abs/1702.05891 Caffe-code:https:// ...
- Java编程的逻辑 (21) - 内部类的本质
本系列文章经补充和完善,已修订整理成书<Java编程的逻辑>,由机械工业出版社华章分社出版,于2018年1月上市热销,读者好评如潮!各大网店和书店有售,欢迎购买,京东自营链接:http:/ ...
- 墨刀 vs Axure RP
https://www.jianshu.com/p/b4b9c1f15304 墨刀https://modao.cc/ Axure RP https://www.axure.com/https://ww ...
- Webpack按需加载一切皆模块
前言 在学习 Webpack 之前,我们需要了解一个概念:模块. 何为模块? 如果你曾学过 Java , C# 之类的语言,一定会知道 Java 中的 import 或 C# 中的 using 吧? ...
- 配置SSH服务使用证书登录Ubuntu服务器
根据项目要求,需要将项目迁移到Linux系统上,作为测试,选用的是阿里云服务器,1核CPU,1G内存(没错就是这么穷),操作系统Ubuntu 16.04 64位.当然其实如果使用阿里云服务器其实是不需 ...
- Ubuntu美化及配置,常见问题解决方案
安装符合审美观,并且具有可用性的Ubuntu桌面,需要耗费一些时间与精力不过,相信我,这值得去做,你会享受这中间的过程,以及最后的成果 首先,我推荐安装的软件列表如下,在安装前,需要先执行以下的步骤: ...
- 循序渐进学.Net Core Web Api开发系列【0】:序言与目录
一.序言 我大约在2003年时候开始接触到.NET,最初在.NET framework 1.1版本下写过代码,曾经做过WinForm和ASP.NET开发.大约在2010年的时候转型JAVA环境,这么多 ...
- [代码审计]云优cms V 1.1.2前台多处sql注入,任意文件删除修复绕过至getshell
0X00 总体简介 云优CMS于2017年9月上线全新版本,二级域名分站,内容分站独立,七牛云存储,自定义字段,自定义表单,自定义栏目权限,自定义管理权限等众多功能深受用户青睐,上线短短3个月,下载次 ...
- hdu 1507 记录路径的二分匹配 **
题意:N*M的矩形,向其中填充1*2的小块矩形,黑色的部分不能填充,问最多可以填充多少块.链接:点我 黑白棋最大匹配 将棋盘中i+j为奇数的做A集合,偶数的做B集合,相邻的则建立联系.于是便转换成寻找 ...
- 一键安装LNMP/LAMP
安装步骤:1.使用putty或类似的SSH工具登陆VPS或服务器: 登陆后运行:yum install screen安装 screen screen -S lnmp创建一个名字为lnmp的会话 2. ...