前几天都跑去快乐学潜水,每天回家还要生出内容,不免的前几篇有些偷懒,当然之後会把前面的一些内容多给补齐,结果让我偷懒了但是我的潜水证照还是没考过呜呜
明天还要打疫苗,感觉会边发烧又边手痛的疯狂学习。
啊啊啊快乐缺氧
回到正题
说到UITableView就是一个很好用的工具
用来作为很多App的呈现
今天要实作的书本图片跟资讯是根据之前自己画的App架构图去实作
这次要用的一样会用到Xib来实作设计
这是典型UITableView呈现
这是我希望呈现的
创建TableViewController的同时,就会一并将UITableViewDelegate & UITableViewDataSource一起建立
那麽什麽是UITableViewDelegate & UITableViewDataSource?
要委托UITableViewController来执行的事情
UITableView的要呈现的资料有什麽
记得在创立UITableViewCell後要记得也创立一个Xib档,用来让Cell做载入
我把我的TableViewCell命名为
TableUIView
我的Xib里面有几样要件
下面整体的框框也是一个UIView包裹Label
愿借阅时长下面的框框是由一个UIView里面套着一个Label
并且记得在相同名字的.swift档案里面加上连结
像之前说的使用UINib,回传一个Nib object
让tableView去使用nib去注册,并给予其IdentifierName
IdentifierName
很重要下面会解释
先做资料赋予
有了这些资料我们就知道要显示哪些东西
// 回传有多少Section会在这个TableView内
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
// 回传会有多少Row在Section里面
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return imagedata.count
}
// TableView的核心,每个TableView呈现的Cell资料
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell:TableUIView = tableView.dequeueReusableCell(withIdentifier: "TTTableUIView", for: indexPath) as! TableUIView
cell.image_1.image = UIImage(named: imagedata[indexPath.row])
cell.bookname.text = booknamedata[indexPath.row]
cell.author.text = authordata[indexPath.row]
cell.owner.text = owners[indexPath.row]
cell.count_day.text = lastdaydata[indexPath.row]
cell.backgroundColor = .init(red: 201, green: 148, blue: 115, alpha: 0)
// Configure the cell...
return cell
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// Configure the cell...
return cell
}
想当初看到这个东西就一脸蒙,完全搞不清楚在干嘛
现在稍微清楚了一点到底是在干嘛
主要就像上面说的"TableView的核心,每个TableView呈现的Cell资料"
还可以透过不同的函数对他做一些动作上的变化
问题来了,有那麽多cell我难道会一个一个做吗,并不会!
会使用像是我上面用的dequeueReusableCell(withIdentifier: "TTTableUIView", for: indexPath)
他的原本的Function长这样
func dequeueReusableCell(withIdentifier identifier: String,
for indexPath: IndexPath) -> UITableViewCell
这是干嘛用的?
ReuseCell(重复使用Cell)
该如何使用?
根据我上面打的,我使用了Register这个方法,让我的cell能够拥有一个IdentifierName
名叫TTTableUIView
所以我要使用我的Cell当作我要重复的Cell
而for indexPath:代表的是说有没有特别指定的位置
回传一个UITableViewCell
// 宣告一个cell
let cell:TableUIView = tableView.dequeueReusableCell(withIdentifier: "TTTableUIView", for: indexPath) as! TableUIView
那麽为什麽後面又要as! TableUIView?
因为我原本回传的是一个UITableViewcell,但是我有自订的TableViewCell
要使用
我使用as!
作为转型,使其变成我自订的TableUIView
因为我的TableUIView
是继承UITableViewCell的子类,所以可以向下转型
// 自订我的cell内容是哪些
cell.image_1.image = UIImage(named: imagedata[indexPath.row])
cell.bookname.text = booknamedata[indexPath.row]
cell.author.text = authordata[indexPath.row]
cell.owner.text = owners[indexPath.row]
cell.count_day.text = lastdaydata[indexPath.row]
cell.backgroundColor = .init(red: 201, green: 148, blue: 115, alpha: 0)
结论:
使用DequeueReusable可以让你重复使用这些View
回传重复的Cell时,记得转型成自己订的Cell
结果:
参考连结
Understanding Custom UIView In-depth: Setting File Owner vs custom class
iOS 开发者指南:透过 Swift 4 学习 Delegates 与 Delegation
Day 16: 来自深渊-UITableView(I) - iT 邦帮忙::一起帮忙解决难题,拯救 IT 人的一天
第九天罗,今天我们要做登入验证,首先我们要先想想,登入要做什麽判断。 打帐号密码,按下登入 确认帐号...
昨天已经将记帐本打造出一个基本雏形了,但似乎功能并不是很多,纯粹就只是看到消费的金额和走向而已,所以...
昨天提到了ReLU还有它的一些variant,那接下来要讲的是另外一个更进阶的想法,叫做Maxou...
tree-二元树的前中後追踪&&最大最小值&树叶 实作练习 说明 实习课的一...
嗨大家!像昨天说的,今天会讲怎麽用 SWR 实作 Notion 部落格的 pagination (分...