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可以寻找标记点,然后与记录边界重新 ...
随机推荐
- js 获取json对象的Key、value(js遍历json对象的key和value)
<script type="text/javascript"> getJson('age'); function getJson(key){ "," ...
- HttpClient 详解一《C#高级编程(第9版)》
1.异步调用 Web 服务 static void Main(string[] args) { Console.WriteLine("In main before call to GetDa ...
- 《jquery实战》javascript 必知必会(2)
A2 一等公民函数 在传统 OO 语言里,对象包含数据和方法.这些语言里,数据和方法通常是不同的概念:javascript另辟蹊径. 与其他 js 的类型一样,函数可以作为对象处理,如String.N ...
- 【BZOJ】4349: 最小树形图
题解 我们只考虑给每个点买一个,之后每个点就可以用最低价格买了 根据最小树形图的算法,就是不断给每个点入度的边找一条最小的 如果构成了树形图就退出,否则把形成了环的点缩成一个点,加上环的权值,然后把指 ...
- Codeforces 652F Ants on a Circle
Ants on a Circle 感觉这个思路好巧妙啊. 我们能发现不管怎么碰撞,初始态和最终态蚂蚁间的相对顺序都是一样的, 并且所占的格子也是一样的, 那么我们就只需要 找到其中一个蚂蚁的最终位置就 ...
- flume修改配置文件
flume修改配置文件后,flume进程会自动将配置文件更新至服务中,同时会初始化日志,重新对于metrics进行记录的. 所以拿api做监控的同学要注意这点啦
- 单页面vue引入百度统计的使用方法!
最近组长安排着做一个项目,h5的应用下载项目,想着做起来还是比较容易,可是看到提出的需求,我就有点懵逼了!需要对应用的下载进行统计!!!我当时就想着我前端怎么对页面点击按钮就行数据统计啊!我以前的项目 ...
- 一个页面从输入URL 到页面加载显示完成,这个过程中都发生了什么?
1.当发送一个URL请求时,浏览器会开启一个线程来处理这个请求,同时在远程DNS服务器上启动一个DNS查询,解析获取网址的IP地址:2.浏览器与远程Web服务器通过TCP三次握手协商来建立一个TCP/ ...
- android 四大组件
韩梦飞沙 韩亚飞 313134555@qq.com yue31313 han_meng_fei_sha 活动,服务,广播接受者,内容提供者. 活动 能够提供 用户界面.服务 没有用户界面.广 ...
- [HDU5714]拍照
[HDU5714]拍照 题目大意: 河上有\(n(n\le10^4)\)个船只,小明希望把尽可能多的船只都完整地拍到一张照片中. 小明位于河的边上,并且可以在河边的任意位置进行拍照,照相机的视野恰好为 ...