Swift纯Code之旅 Day18. 「选取TableViewCell」

前言

「重复」页面的画面已经完成了,接着实作功能吧!
功能图:

实作

  1. 首先建立一个变数,用来储存Cell是否被点击过
var isSelected: [Int] = []

2.在「RepeatAlarmViewController」的TableView didSelectRowAt()中,
新增以下程序码,判断该栏Cell是否有点击过

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        // 判断目前点击的Cell是否有储存於阵列中,有存在阵列中代表有点击过
        if self.isSelected.contains(indexPath.row) {
            // 若已选择过,则将该index移除阵列内
            self.isSelected = self.isSelected.filter{$0 != indexPath.row}
        } else {
            // 若未选择过,则将该index加入阵列中
            self.isSelected.append(indexPath.row)
        }
        tableView.reloadRows(at: [indexPath], with: .automatic)
    }

3.在TableView的cellForRowAt中,新增以下程序码,用来显示已选取的Cell打勾的效果

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard let cell = tableView.dequeueReusableCell(withIdentifier: RepeatAlarmTableViewCell.identifier, for: indexPath) as? RepeatAlarmTableViewCell else {  return UITableViewCell() }
        
        cell.titleLabel.text = titleDatas[indexPath.row]
        
        // 预设Cell为没被点击过
        cell.selectionStyle = .none
        
        // 判断Cell是否有在阵列中,有则打勾,没有则不打勾
        if self.isSelected.contains(indexPath.row) {
            cell.accessoryType = .checkmark
        } else {
            cell.accessoryType = .none
        }
        
        return cell
    }

现在来看一下效果

Cool~ 明天来实作将所选的资料回传的部分


<<:  Day 13 Component Lifecycle -2

>>:  [Day 13] 资料增强 — 我全都要.jpg

Day28 X Runtime Performance Debugging

提到 Web 前端的效能优化,有许多的技巧是聚焦在如何减少页面的「载入时间 Loading Tim...

什麽是零信任(What is Zero Trust)?

零信任是一种网络安全范式,用於支持可见性的细粒度,动态和以数据为中心的访问控制。 (访问控制基於需要...

D30: 工程师太师了: 第16话

工程师太师了: 第16话 杂记: 今天终於是第三十天了, 漫长的三十天, 每天都要努力发文, 有监於...

企划实现(6)

甚麽是第三方支付? 第三方支付是指电子商务企业或是具实力及信用保障的独立机构,与银行之间建立一个中立...

【Day11】HomeFragment X RecyclerView X Firestore取/删除资料

既然我们都已经有了上传资料,当然我们也要有可以看我们所有上架内容的地方,还有下架资料的地方啦!! ...