优化tableView加载cell与model的过程
优化tableView加载cell与model的过程
效果图
说明
1. 用多态的特性来优化tableView加载cell与model的过程
2. swift写起来果然要比Objective-C简洁了不少
源码
https://github.com/YouXianMing/Swift-TableViewDemo
https://github.com/YouXianMing/OC-TableViewDemo
//
// ViewController.swift
// Swift-TableViewDemo
//
// Created by YouXianMing on 15/9/28.
// Copyright © 2015年 YouXianMing. All rights reserved.
// import UIKit class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { let typeOneCellFlag : String! = "typeOneCellFlag"
let typeTwoCellFlag : String! = "typeTwoCellFlag" var datasArray : NSMutableArray!
var tableView : UITableView! override func viewDidLoad() { super.viewDidLoad() self.initDatasArray() self.initTableView()
} // 数据源相关
func initDatasArray() { datasArray = NSMutableArray()
datasArray.addObject(TypeOneModel(flag: typeOneCellFlag, cellHeight: , data: "TypeOneModel"))
datasArray.addObject(TypeTwoModel(flag: typeTwoCellFlag, cellHeight: , data: "TypeTwoModel"))
} // tableView相关
func initTableView() { tableView = UITableView(frame: view.bounds, style: .Plain)
tableView.delegate = self
tableView.dataSource = self
tableView.separatorStyle = .None
view.addSubview(tableView) tableView.registerClass(TypeOneCell.classForCoder(), forCellReuseIdentifier: typeOneCellFlag)
tableView.registerClass(TypeTwoCell.classForCoder(), forCellReuseIdentifier: typeTwoCellFlag)
} func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return datasArray.count
} func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let model : BaseModel! = datasArray[indexPath.row] as! BaseModel let cell : BaseTableViewCell! = tableView.dequeueReusableCellWithIdentifier(model.cellFlag!) as! BaseTableViewCell
cell.tableView = tableView
cell.indexPath = indexPath
cell.data = model.data
cell.loadData() return cell
} func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { let model : BaseModel! = datasArray[indexPath.row] as! BaseModel return model.cellHeight!
}
}
细节
优化tableView加载cell与model的过程的更多相关文章
- plist文件的读取和xib加载cell
plist 文件读取 例如在工程里倒入了plist文件 在工程里需要用到plist文件里的信息,就需要把plist文件读取出来. 如程序: -(NSArray *)moreDataArr{ if (! ...
- [转]JavaScript 的性能优化:加载和执行
原文链接:http://www.ibm.com/developerworks/cn/web/1308_caiys_jsload/index.html?ca=drs- JavaScript 的性能优化: ...
- 《前端之路》之 前端图片 类型 & 优化 & 预加载 & 懒加载 & 骨架屏
目录 09: 前端图片 类型 & 优化 & 预加载 & 懒加载 & 骨架屏 09: 前端图片 类型 & 优化 & 预加载 & 懒加载 & ...
- 【转】js JavaScript 的性能优化:加载和执行
JavaScript 的性能优化:加载和执行 转自:https://www.ibm.com/developerworks/cn/web/1308_caiys_jsload/ 随着 Web2.0 技术的 ...
- Qt tableview加载数据
Qt tableview加载数据 //把数据加载到tableView void ImportData::loadDataInTableView() { ) { if (pageNum>stude ...
- tableView 加载更多
在ios开中中,由于屏幕尺寸限制,如果需要显示的数据很多,需要用到分页加载. 原理:先数据放到一个table中,先显示10条,table底部有一察看更多选项,点击察看更多查看解析的剩余数据.基本上就是 ...
- [Tensorflow] 使用 tf.train.Checkpoint() 保存 / 加载 keras subclassed model
在 subclassed_model.py 中,通过对 tf.keras.Model 进行子类化,设计了两个自定义模型. import tensorflow as tf tf.enable_eager ...
- Android WebView 优化页面加载效果
目前带有Web功能的APP越来越多,为了能够更好的使用WebView展示页面,可以考虑做相关的优化:WebView 缓存,资源文件本地存储,客户端UI优化. 可能有些人会说,为什么不做Native的, ...
- 浏览器加载和渲染HTML的过程(标准定义的过程以及现代浏览器的优化)
先看一下标准定义的浏览器渲染过程(网上找的): 浏览器打开网页的过程 用户第一次访问网址,浏览器向服务器发出请求,服务器返回html文件: 浏览器开始载入html代码,发现 head 标签内有一个 l ...
随机推荐
- java将System.out.println的输出导出到文件中
直接看代码 public static void saveStreamToFile(String savePath,String input){ try { //savePath like c:/lo ...
- Linux的MySQL不能远程访问
1.首先,你要确认用户是否只允许localhost访问: 在linux下登录mysql mysql -uroot -p密码; use mysql; select `host`,`use ...
- koa2开发入门
一.koa2入门 1.创建koa2工程 首先,我们创建一个目录hello-koa并作为工程目录用VS Code打开.然后,我们创建app.js,输入以下代码: // 导入koa,和koa 1.x不同, ...
- Python调用nmap扫描网段主机信息生成xml
#!/usr/bin/env python # -*- coding: utf_8 -*- # Date: 2015年10月23日 # Author:蔚蓝行 # 博客 http://www.cnblo ...
- [转]COPY OR MOVE FILES AND FOLDERS USING OLE AUTOMATION
本文转自:http://sqlindia.com/copy-move-files-folders-using-ole-automation-sql-server/ I love playing aro ...
- C#在.NET编译执行过程
1..NET语言的编译器接受源代码文件,并生成名为程序集的输出文件. 程序集要么是可执行的,要么是DLL 程序集里的代码并不是本机代码,而是一种名称为CIL的中间语言 程序集包含如下信息: 程序的CI ...
- 【转】到底什么时候应该用MQ
原文地址:http://zhuanlan.51cto.com/art/201704/536407.htm 一.缘起 一切脱离业务的架构设计与新技术引入都是耍流氓. 引入一个技术之前,首先应该解答的问题 ...
- Spring系列之——使用了哪些设计模式
1 工厂模式:BeanFactory.ApplicationContext创建中 2 模板模式:BeanFactory.ApplicationContext实现中 3 代理模式:在AOP实现中用到了J ...
- 安装pl/sql developer(内附下载地址)
前言:PL/SQL Developer是一个集成开发环境,更方便的使用oracle,这里记录一下安装过程. 第一步:下载 这里提供我的百度云连接: 链接:https://pan.baidu.com/s ...
- python中垃圾回收机制
Python垃圾回收机制详解 一.垃圾回收机制 Python中的垃圾回收是以引用计数为主,分代收集为辅.引用计数的缺陷是循环引用的问题.在Python中,如果一个对象的引用数为0,Python虚拟 ...