Swift学习笔记39-实战注意事项

注意事项:

  • 注意:一些需要释放的页面,尽量避免调用全局变量!!!

  • Cell的事件就比如didselect是在最下面一层,要是上面有其他uicontrol或者uibutton会把事件抢走!!!

  • 能调用系统API的地方尽量调用系统的API,不要自己写!!!就比如TableView里的didSelect是控制点击事件的,需要点击整块Cell的时候,直接用这个函数就好了。

  • 千万不要使用AutoLayout,非常多问题!实在不行就用SwiftUI嵌套

  • 本地化文本不要设置comment内容

  • 数组越界是一个大问题,千万要注意⚠️就比如现在要用第五位,必须先确保第五位存在。

  • 分割字符串后使用map映射到一个数组中的API

    swift 复制代码
    import Foundation
    
    let str = "apple,banana,orange"
    
    // components 直接返回 [String] 数组
    let result: [String] = str.components(separatedBy: ",")
                              .map { $0.capitalized }
    
    print(result) 
    // 输出: ["Apple", "Banana", "Orange"]
  • TableView的Cell高度控制,最好还是用一个Cell内的函数来控制高度和内部元素的变化,然后再在渲染cell的时候使用它,不然会出很多Bug。而控制状态的bool变量最好是在TableView的VC里,有多个Cell最好分别存储它们的状态, 就像Bool

  • 为了避免史山代码的形成,没有使用过,也不从未上线的代码,就将其删除。注意,曾在线上版本里使用过的代码绝对不可以删除,注释掉就好了。

  • 避免在重用机制下使用临时变量,就比如TableView

    tableView.reconfigureRows(at: [indexPath])

    就比如这个,可能会出未知的问题,既然弱引用了Self,后面刷新的时候就继续用self引用其他组件就好了。

    swift 复制代码
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let base = baweiQABases[indexPath.row]
            let isExpanded = isExpandeds[indexPath.row]
            let cell: BaweiQACell = tableView.cellForRow(at: indexPath) as? BaweiQACell ?? BaweiQACell(style: .default, reuseIdentifier: "BaweiQACell")
            cell.question_content_label.text = base.questions
            cell.answers_content_label.text = base.answers
    //        cell.answers_content_label.setLineHeight(24)
            
            let q_size = YK_Toast.getStrSize(text: base.questions, font: .systemFont(ofSize: 14), size: .init(width: SCREENWIDTH - 64, height: 600))
    //        let a_size = YK_Toast.getStrSize(text: base.answers, font: .systemFont(ofSize: 14), size: .init(width: SCREENWIDTH - 64, height: 600))
            let a_size = YK_Toast.getStrSize(text: base.answers, font: .systemFont(ofSize: 14), size: .init(width: SCREENWIDTH - 64, height: 1000), lineSpacing: 4)
            let expanded_false_backView_size: CGRect = .init(x: GAP_16, y: 0, width: SCREENWIDTH - GAP_32, height: 16 + 30 + 8 + q_size.height + 16 + 0.5 + 16 + 18 + 16)
            let expanded_true_backView_size: CGRect = .init(x: GAP_16, y: 16 + 30 + 8 + q_size.height + 24 + 30 + 8 + a_size.height + 16 + 0.5 + 16, width: SCREENWIDTH - GAP_32, height: 16 + 30 + 8 + q_size.height + 16 + 0.5 + 16 + 18 + 16 + (24 + 30 + 8 + a_size.height))
            
            cell.selectionStyle = .none
            cell.backgroundColor = .clear
            cell.question_content_label.frame = .init(x: 16, y: 16 + 30 + 8, width: SCREENWIDTH - 64, height: q_size.height)
            cell.backView.frame = expanded_false_backView_size
            cell.dividerView.frame = .init(x: 16, y: 16 + 30 + 8 + q_size.height + 16, width: SCREENWIDTH - 64, height: 0.5)
            cell.expend_answer.frame = .init(x: 16, y: 16 + 30 + 8 + q_size.height + 16 + 16, width: SCREENWIDTH - 64, height: 18)
            cell.configure(with: isExpanded, q_size: q_size, a_size: a_size)
            cell.clickBlock = { [weak self] in
                self?.isExpandeds[indexPath.row] = !isExpanded
    //            tableView.reconfigureRows(at: [indexPath])
                self?.homeScroll.reloadRows(at: [indexPath], with: .automatic)
                #warning("3.5修改")
                //尽量避免在重用机制的条件下使用临时变量
            }
            return cell
        }
  • 做展开收起效果,做到操作后实时刷新TableVIew高度的API

    主要有这两个
    1.

    swift 复制代码
    self?.homeScroll.reconfigureRows(at: [indexPath])
    swift 复制代码
    self?.homeScroll.reloadRows(at: [indexPath], with: .automatic)
相关推荐
海带紫菜菠萝汤1 小时前
MSE (Media Source Extensions) 实战:流媒体分块加载与自适应码率
前端·javascript·音视频
@小匠1 小时前
Spring Boot Nacos绑定 Map 时中文 key 导致启动失败:一次从复现到源码的排查实录
java·开发语言·前端
黄华SJ520it1 小时前
顶俏洗衣液模式制度开发介绍:S2B2C社交分销+多门店核销系统全解析
前端·数据库·小程序·零售·系统开发
神明不懂浪漫1 小时前
【第五章】队列
开发语言·数据结构·经验分享·笔记·算法
晓说前端2 小时前
TypeScript 核心语法应用 —— Vue 3 中的使用(上)
前端·typescript
No Silver Bullet2 小时前
Vue进阶(贰幺叁)vue.config.js 中 productionSourceMap 作用详解
前端·javascript·vue.js
xcsweb2 小时前
从5分钟到10秒:我用一个Skill把团队部署效率提升了30倍
前端·vue.js
闪闪发亮的小星星2 小时前
STK中计算两星距离
笔记
用户62960593247052 小时前
一次位置调整引发的画布失忆:深入 Vue2 虚拟 DOM 复用机制
前端·vue.js