注意事项:
-
注意:一些需要释放的页面,尽量避免调用全局变量!!!
-
Cell的事件就比如didselect是在最下面一层,要是上面有其他uicontrol或者uibutton会把事件抢走!!!
-
能调用系统API的地方尽量调用系统的API,不要自己写!!!就比如TableView里的didSelect是控制点击事件的,需要点击整块Cell的时候,直接用这个函数就好了。
-
千万不要使用AutoLayout,非常多问题!实在不行就用SwiftUI嵌套
-
本地化文本不要设置comment内容
-
数组越界是一个大问题,千万要注意⚠️就比如现在要用第五位,必须先确保第五位存在。
-
分割字符串后使用map映射到一个数组中的API
swiftimport 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引用其他组件就好了。
swiftfunc 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.swiftself?.homeScroll.reconfigureRows(at: [indexPath])swiftself?.homeScroll.reloadRows(at: [indexPath], with: .automatic)