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加载网络数据的优化的更多相关文章

  1. Android之ListView&Json加载网络数据

    使用到的主要内容: 1.Json 解析网络数据 2.异步任务加载图片和数据 3.ListView 的内存空间优化(ConvertView)和运行时间优化(ViewHolder) 4.ListView ...

  2. android122 zhihuibeijing 新闻中心NewsCenterPager加载网络数据实现

    新闻中心NewsCenterPager.java package com.itheima.zhbj52.base.impl; import java.util.ArrayList; import an ...

  3. iOS中 UIWebView加载网络数据 技术分享

    直奔核心: #import "TechnologyDetailViewController.h" #define kScreenWidth [UIScreen mainScreen ...

  4. NSURLConnection 异步加载网络数据

    #import "ViewController.h" @interface ViewController () @end @implementation ViewControlle ...

  5. 鸿蒙的fetch请求加载聚合数据的前期准备工作-手动配置网络权限

    目录: 1.双击打开"config.json"文件 2.找到配置网络访问权限位置1 3.配置内容1 4.默认访问内容是空的 5.添加配置内容2 6.复制需要配置的网络二级URL 7 ...

  6. ios网络学习------4 UIWebView的加载本地数据的三种方式

    ios网络学习------4 UIWebView的加载本地数据的三种方式 分类: IOS2014-06-27 12:56 959人阅读 评论(0) 收藏 举报 UIWebView是IOS内置的浏览器, ...

  7. DevExpress ChartControl大数据加载时有哪些性能优化方法

    DevExpress ChartControl加载大数据量数据时的性能优化方法有哪些? 关于图表优化,可从以下几个方面解决: 1.关闭不需要的可视化的元素(如LineMarkers, Labels等) ...

  8. js防抖和节流优化浏览器滚动条滚动到最下面时加载更多数据

    防抖和节流,主要是用来防止过于平凡的执行某个操作,如浏览器窗口变化执行某个操作,监听某个input输入框keyup变化,瀑布流布局时Y轴滚动,图片加载. js函数的防抖 经过一段事件才执行某个操作,如 ...

  9. Learning Spark中文版--第五章--加载保存数据(2)

    SequenceFiles(序列文件)   SequenceFile是Hadoop的一种由键值对小文件组成的流行的格式.SequenceFIle有同步标记,Spark可以寻找标记点,然后与记录边界重新 ...

随机推荐

  1. 4.自定义数据《jquery实战》

    4.4 元素中的存储自定义数据 data([key],[value]) 在元素上存放数据,返回jQuery对象. key (String) 存储的数据名. key,value (String,Any) ...

  2. Connect to DB2 database in eclipse via jdbc

    https://stackoverflow.com/questions/23801841/connect-to-db2-database-in-eclipse-via-jdbc

  3. sqoop1.4.6数据迁移

    sqoop介绍 sqoop是apache旗下一款“Hadoop和关系数据库服务器之间传送数据”的工具. 导入数据:MySQL,Oracle导入数据到Hadoop的HDFS.HIVE.HBASE等数据存 ...

  4. rabbitmq学习(四) —— 发布订阅

    为了说明这种模式,我们将建立一个简单的日志系统.这个系统将由两个程序组成,第一个将发出日志消息,第二个将接收并处理日志消息.在我们的日志系统中,每一个运行的接收程序的副本都会收到日志消息. 交换器(E ...

  5. 七夕情人节表白-纯JS实现3D心形+图片旋转

    七夕情人节就快到了,这里献上纯js表白神器-心里都是你,预览: 技术点:css-3d.js-随机色.js-transform 1.html: <div class="heart&quo ...

  6. Linux驱动之平台设备

    <平台设备设备驱动> a:背景: 平台总线是Linux2.6的设备驱动模型中,关心总线,设备和驱动这3个实体.一个现实的Linux设备和驱动通常需要挂接在一种总线上(比如本身依附于PCI, ...

  7. WordPress 建站教程:新手搭建 WordPress个人博客图文教程(完全版)

    前言 WordPress 作为动态博客的代表,至今已经有十几年历史,而且一直在更新发展中,功能强大,插件和主题丰富,WordPress搭建使用也很方便.作为个人站长和博主,很多都是从 WordPres ...

  8. BZOJ.2246.[SDOI2011]迷宫探险(DP 记忆化搜索 概率)

    题目链接 求最大的存活概率,DP+记忆化. 用f[s][x][y][hp]表示在s状态,(x,y)点,血量为hp时的存活概率. s是个三进制数,记录每个陷阱无害/有害/未知. 转移时比较容易,主要是在 ...

  9. 深入理解指针—>指针函数与函数指针的区别

    一. 在学习过程中发现这"指针函数"与"函数指针"容易搞错,所以今天,我自己想一次把它搞清楚,找了一些资料,首先它们之间的定义: 1.指针函数是指带指针的函数, ...

  10. Codeforces Round #396 (Div. 2) E. Mahmoud and a xor trip dfs 按位考虑

    E. Mahmoud and a xor trip 题目连接: http://codeforces.com/contest/766/problem/E Description Mahmoud and ...