优化tableView加载cell与model的过程
优化 过程 加载 model cell tableView
2023-09-14 08:57:30 时间
优化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: 100, data: "TypeOneModel")) datasArray.addObject(TypeTwoModel(flag: typeTwoCellFlag, cellHeight: 200, 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! }
细节
相关文章
- EasyNVR网页/微信播放RTSP摄像机HLS/RTMP播放时出现起播等待问题的优化过程
- EasyNVR网页/微信播放RTSP摄像机HLS/RTMP播放时出现起播等待问题的优化过程
- MySQL内核月报 2014.11-MySQL· 性能优化·hash_scan 算法的实现解析
- SEO优化篇——meta用法
- DL之CNN:计算机视觉之卷积神经网络算法的简介(经典架构/论文)、CNN优化技术、调参学习实践、CNN经典结构及其演化、案例应用之详细攻略
- 基于强化学习的电动汽车的储能系统优化控制和存储容量优化(Matlab代码实现)
- 【无线通信】鲸鱼优化算法及其在无线网络资源分配中的应用(Matlab代码实现)
- 智能优化算法应用:基于麻雀搜索算法PID参数优化 - 附代码
- 【人工智能大模型】一文彻底讲透——什么是 PPO(Proximal Policy Optimization,近端策略优化)?
- 【成为架构师课程系列】性能优化技术之“池化技术”:如何减少频繁创建数据库连接的性能损耗?
- memcpy内存拷贝及优化策略图解
- 【2023 年第十三届 MathorCup 高校数学建模挑战赛】A 题 量子计算机在信用评分卡组合优化中的应用 详细建模过程解析及代码实现
- CentOS系统安装之后的优化