优化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的过程的更多相关文章

  1. plist文件的读取和xib加载cell

    plist 文件读取 例如在工程里倒入了plist文件 在工程里需要用到plist文件里的信息,就需要把plist文件读取出来. 如程序: -(NSArray *)moreDataArr{ if (! ...

  2. [转]JavaScript 的性能优化:加载和执行

    原文链接:http://www.ibm.com/developerworks/cn/web/1308_caiys_jsload/index.html?ca=drs- JavaScript 的性能优化: ...

  3. 《前端之路》之 前端图片 类型 & 优化 & 预加载 & 懒加载 & 骨架屏

    目录 09: 前端图片 类型 & 优化 & 预加载 & 懒加载 & 骨架屏 09: 前端图片 类型 & 优化 & 预加载 & 懒加载 & ...

  4. 【转】js JavaScript 的性能优化:加载和执行

    JavaScript 的性能优化:加载和执行 转自:https://www.ibm.com/developerworks/cn/web/1308_caiys_jsload/ 随着 Web2.0 技术的 ...

  5. Qt tableview加载数据

    Qt tableview加载数据 //把数据加载到tableView void ImportData::loadDataInTableView() { ) { if (pageNum>stude ...

  6. tableView 加载更多

    在ios开中中,由于屏幕尺寸限制,如果需要显示的数据很多,需要用到分页加载. 原理:先数据放到一个table中,先显示10条,table底部有一察看更多选项,点击察看更多查看解析的剩余数据.基本上就是 ...

  7. [Tensorflow] 使用 tf.train.Checkpoint() 保存 / 加载 keras subclassed model

    在 subclassed_model.py 中,通过对 tf.keras.Model 进行子类化,设计了两个自定义模型. import tensorflow as tf tf.enable_eager ...

  8. Android WebView 优化页面加载效果

    目前带有Web功能的APP越来越多,为了能够更好的使用WebView展示页面,可以考虑做相关的优化:WebView 缓存,资源文件本地存储,客户端UI优化. 可能有些人会说,为什么不做Native的, ...

  9. 浏览器加载和渲染HTML的过程(标准定义的过程以及现代浏览器的优化)

    先看一下标准定义的浏览器渲染过程(网上找的): 浏览器打开网页的过程 用户第一次访问网址,浏览器向服务器发出请求,服务器返回html文件: 浏览器开始载入html代码,发现 head 标签内有一个 l ...

随机推荐

  1. java学习-AES加解密之AES-128-CBC算法

    AES算法简介 AES是一种对称加密算法,或称分组对称加密算法.  是Advanced Encryption Standard高级加密标准,简称AES AES的基本要求是,采用对称分组密码体制.分组密 ...

  2. springcloud-03-服务注册

    新建一个 provider-user 和consumer-movie, user为服务提供者, movie为服务的消费真, 没有什么难的, 直接上代码 microserver-provider-use ...

  3. PTA (Advanced Level) 1027 Colors in Mars

    Colors in Mars People in Mars represent the colors in their computers in a similar way as the Earth ...

  4. 任务四十一:UI组件之日历组件(二)

    任务四十一:UI组件之日历组件(二) 面向人群: 有一定基础的同学 难度: 中 重要说明 百度前端技术学院的课程任务是由百度前端工程师专为对前端不同掌握程度的同学设计.我们尽力保证课程内容的质量以及学 ...

  5. Git报错:error: cannot open .git/FETCH_HEAD: Read-only file system

    Git:git pull时报错 error: cannot open .git/FETCH_HEAD: Read-only file system 查看该文件: 未在网上找到解决办法,重启服务器就好了 ...

  6. JavaScript 正则表达式RegExp 和字符串本身的正则表达式

    JavaScript 正则表达式 正则表达式(英语:Regular Expression,在代码中常简写为regex.regexp或RE)使用单个字符串来描述.匹配一系列符合某个句法规则的字符串搜索模 ...

  7. ASP.NET MVC 学习笔记-1.ASP.NET MVC 基础

    ASP.NET MVC在原来ASP.NET的基础上抛弃了基于页面的架构风格,使用了全新的MVC(模型-视图-控制器)架构的一种技术. 目前,它和ASP.NET都共存在.NET Framework之上. ...

  8. 机器学习classification_report方法及precision精确率和recall召回率 说明

    classification_report简介 sklearn中的classification_report函数用于显示主要分类指标的文本报告.在报告中显示每个类的精确度,召回率,F1值等信息. 主要 ...

  9. Access restriction: The type BASE64Encoder is not accessible due to restrict(转载)

    Access restriction: The type BASE64Encoder is not accessible due to restrict 2011年11月18日 20:47:06 阅读 ...

  10. java并发编程的艺术(二)---重排序与volatile、final关键字

    本文来源于翁舒航的博客,点击即可跳转原文观看!!!(被转载或者拷贝走的内容可能缺失图片.视频等原文的内容) 若网站将链接屏蔽,可直接拷贝原文链接到地址栏跳转观看,原文链接:https://www.cn ...