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可以寻找标记点,然后与记录边界重新 ...
随机推荐
- liunx jdk安装
打开https://www.oracle.com/technetwork/java/javase/downloads/index.html 选择Development版本(server为服务器版本), ...
- 大数据统计分析平台之一、Kafka单机搭建
1.zookeeper搭建 Kafka集群依赖zookeeper,需要提前搭建好zookeeper 单机模式(7步)(集群模式进阶请移步:http://blog.51cto.com/nileader/ ...
- PLSQL Developer 中文显示乱码的解决方法
PLSQL Developer 中文显示乱码是因为 Oracle 数据库所用的编码和 PLSQL Developer 所用的编码不同所导致的. 解决方法: 1. 先查询 Oracle 所用的编码 se ...
- spark sql中保存数据的几种方式
从官网来copy过来的几种模式描述: Scala/Java Python Meaning SaveMode.ErrorIfExists(default) "error"(defau ...
- 【翻译】 What is class diagram(什么是类图)?
[翻译] What is class diagram(什么是类图)? 写在翻译之前 这是一篇关于UML的英文博客的翻译,是我们的老师在教授我们UML类图的时候推荐给我们的,为了学习UML顺便学习英语, ...
- C# 判断字符编码的六种方法
方法一http://blog.csdn.net/qiujiahao/archive/2007/08/09/1733169.aspx在unicode 字符串中,中文的范围是在4E00..9FFF:CJK ...
- CodeSmith 基础用法和例子
〇. 前言 一. 工具设置 CodeSmith默认是不支持中文的,那么我们必须要先设置使其支持中文显示,保存.并且要能够在生成文件中支持中文. [Tools ...
- HDU 4786 Fibonacci Tree (2013成都1006题)
Fibonacci Tree Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- leetcode第一刷_N-Queens II
这个题好无趣,竟然输出解的个数.前一个题把全部解都输出出来了.还愁不知道解的个数吗. . 我怀疑这个解的个数是有一个类似通项的东西,就上网查了一下.没有啊亲,最后就把上一题的代码略微改了一下过掉了. ...
- How to detect the types of executable files
How to detect the types of executable files type { IMAGE_DOS_HEADER: DOS .EXE header. } IMAGE_DOS_HE ...