当前位置:网站首页>UITableView 基本使用和优化
UITableView 基本使用和优化
2022-07-27 14:55:00 【CAir2】
优化:局部刷新,时刻谨记View和数据源分离
继承UITableViewDataSource和UITableViewDelegate然后指定数据源:
_tableView.backgroundColor = AppColor.main_them
_tableView.delegate = self
_tableView.dataSource = self
_tableView.separatorStyle = .none
//GameListViewCell派生UITableViewCell
//forCellReuseIdentifier作为一类Cell的标记,随便定义
//关于register方法,本人不太推荐,不好维护
//_tableView.register(GameListViewCell.self, forCellReuseIdentifier: "list_speed_cell")
重写UITableViewDataSource指定数据的种类和数量以及根据forCellReuseIdentifier返回对应的cell
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
//返回section中的行数量
return _gameData.count
}
//返回cell
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
//网上很多博客的用法都是使用dequeueReusableCell
//在这里不建议,一是不方便维护,二是如果存在滑动删除的话,在刷新的时候删除会缩回
//这里本人建议使用一个Cell池,大小为当前窗口可显示的大小的2倍即可。
//刷新局部或者指定Cell的时候,遍历Cell池查找指定的Cell刷新即可,因为如果用户可见,则Cell一定在池子中
//let cell = tableView.dequeueReusableCell(withIdentifier: "list_speed_cell",for:indexPath) as! GameListViewCell
let cell = _cellPool[indexPath.row%_cellPoolMax]
cell._delegate = self
cell.setData(gameItem: _gameData[indexPath.row])
print("cell row:\(indexPath.row),selection:\(indexPath.section)")
return cell;
}
如果需要自定义Cell的话,可以直接从UITableViewCell派生,然后在构造里面初始化UI,在layoutSubviews里面进行布局即可。
class GameListViewCell: UITableViewCell {
override func layoutSubviews() {
super.layoutSubviews()
//进行子控件布局
}
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
initView()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
initView()
}
private func initView() {
//进行UI规划,关于UI布局篇可以参考
//https://blog.csdn.net/CAir2/article/details/120198494
}
}
至于更多关于Selection和Row之间的关系以及自定义UI,可以自行学习。
边栏推荐
- Flex弹性盒布局2
- my_ Ls summary
- Array analog linked list
- Niuke topic - parity rearrangement of linked list, right view of output binary tree, bracket generation, first non repeating character in character stream
- ES6数组新增属性
- Rotate the whole model in ADAMS
- If you don't want to step on those holes in SaaS, you must first understand the "SaaS architecture"
- Stylelint check error: unexpected missing generic font family font family no missing generic family keyword
- Matlab legend usage
- Storage of data in C language
猜你喜欢

Data collection: skillfully using Bloom filter to extract data summary

OpenCV(三)——图像分割

Matlab legend usage

Shardingsphere-proxy-5.0.0 distributed snowflake ID generation (III)

牛客题目——链表的奇偶重排、输出二叉树的右视图、括号生成、字符流中第一个不重复的字符

Character function, string function and memory function of C language

牛客题目——对称的二叉树

ShardingSphere-proxy-5.0.0分布式雪花ID生成(三)

Operators of C language

了解Bom与DOM的基本属性
随机推荐
ROS - error in running.Py file in the project
Great Cells & Counting Grids
C语言之函数
LNMP environment - deploy WordPress
Data collection: skillfully using Bloom filter to extract data summary
Servlet uses cookies to realize the last login time of users
C语言之结构体及位段
OpenCV(三)——图像分割
C语言之操作符
Build a cloud native file system for media assets
[paper reading] transformer with transfer CNN for remote sensing imageobject detection
Get the array list of the previous n days and the previous and subsequent days of the current time, and cycle through each day
Jerry's book can't find Bluetooth solutions [article]
从零开始Blazor Server(1)--项目搭建
ES6数组的方法及伪数组转数组方法
Leader: who uses redis overdue monitoring to close orders and get out of here!
Stylelint check error: unexpected missing generic font family font family no missing generic family keyword
Polynomial locus of order 5
Dynamic memory allocation in C language
jsp-El表达式,JSTL标签