当前位置:网站首页>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,可以自行学习。
边栏推荐
- In addition to "adding machines", in fact, your micro service can be optimized like this
- Layoff quarrel, musk: I'm too hard; Mercilessly open source a public opinion acquisition project; Feature engineering is as simple as parameter adjustment?! Nerf boss shouted that he couldn't move; Cu
- 从零开始Blazor Server(1)--项目搭建
- String numeric type converted to thousands
- android中的图片三级缓存
- Fast Planner - detailed explanation of kinetic astar
- Advanced pointer of C language
- C语言之字符函数和字符串函数及内存函数
- Servlet Chinese garbled setcontenttype setting is invalid
- C语言之结构体及位段
猜你喜欢
随机推荐
LOJ 510 - "libreoj noi round 1" memories outside the north school gate [line segment tree]
Enumeration and union of C language
AS更换背景主题以及背景图片
Automatic classification of e-commerce UGC pictures using Baidu PaddlePaddle easydl
Simulation generate report
Quadratic programming based on osqp
Insert pictures in word to maintain high DPI method
If you don't want to step on those holes in SaaS, you must first understand the "SaaS architecture"
C语言之指针初级
Share a scheme of "redis" to realize "chat round system" that can't be found on the Internet
Advanced pointer of C language
Interpretation of C basic syntax: summarize some commonly used but easily confused functions (i++ and ++i) in the program (bit field)
js中的函数
Ten thousand words analysis ribbon core components and operation principle
牛客题目——判断是不是完全二叉树、平衡二叉树
Getting started with nvida CUDA dirverapi
Layoff quarrel, musk: I'm too hard; Mercilessly open source a public opinion acquisition project; Feature engineering is as simple as parameter adjustment?! Nerf boss shouted that he couldn't move; Cu
MPC5744p时钟模块
CDQ divide and conquer and whole dichotomy learning notes
合工大苍穹战队视觉组培训Day7——视觉,jetson naon与D435i









