一、寫在開頭的話,為節省資源,請遵守APPLE的原則:
原則1、CELL一定要復用
原則2、identifier也要相同一致
原則3、請參考以上原則
二、重用機制問題思考?
首先如果你是apple,你不希望一個應用里面有太多cell對象,影響內存,如果上百個cell,如果按view的方式,那么就有上百個實例對象。 apple肯定是不希望這么做的,不符合設計思想,如果是你,你會怎么做? 這個問題對于有些數量比較多的cell是必須解決的。
那么我如果是apple,我會想, 對于設備而言,物理尺寸是限制了的,首先我必須創建屏幕尺寸所容納的個數的cell ,這點沒辦法改變 。 但是在下滑的時候,將要展示更多cell的時候,我事先不會全部加載出來,對于內存壓力太大, 那么,我就預先展示一個就可以了,作為響應的緩沖。
三、直接上代碼,很感謝Simpon提供的思路(http://bbs.itheima.com/thread-331532-1-1.html?iosxp)
1,移除UIButton、UIImageView,UILabel
給自定義添加的Button添加一個tag 。根據tag移除
2,創建新UIButton、UIImageView,UILabel
創建新Button的時候,我也添加相同的tag,為了下次復用的時候,好移除
3, 添加到cell
(一)、Controller.swift 關鍵部份
override func viewDidLoad() {
super.viewDidLoad()
self.clearsSelectionOnViewWillAppear = true
// self.tableView.cont = UIColor.lightGray
self.tableView.tableFooterView?.frame = CGRect.zero
self.tableView.emptyDataSetSource = self
self.tableView.emptyDataSetDelegate = self
// 手工建立 tableViewCell的話,就要注冊 cell 了
tableView.register(LotteryHotCell.self, forCellReuseIdentifier: "HotCell")
tableView.estimatedRowHeight = 150.0
tableView.rowHeight = UITableViewAutomaticDimension
tableView.tableFooterView = UIView()
tableView.separatorStyle = .none
// 加載數據
initData(isPullUp: false)
// 刷新數據
initRefresh()
// 視圖自動調整
self.view.layoutIfNeeded()
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// FIXME: 不重用表格數據 數據多的放可以升級為 collectionView 06.07.2017 ,但會嚴重卡屯,要升級
let cell = tableView.dequeueReusableCell(withIdentifier: "HotCell", for: indexPath) as! LotteryHotCell
cell.lotteryHotDataList = self.lotteryHotViewModel.lotteryHotDataList[indexPath.row]
cell.frame = tableView.bounds
cell.layoutIfNeeded()
return cell
}
(二)、tableViewCell 部份
import UIKit
import SwifterSwift
class HotCell: UITableViewCell,UICollectionViewDelegate, UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {
// 初始化
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
self.collectionView = UICollectionView(frame: CGRect(x: 8, y: 40, width:kScreenW - 30, height: 40), collectionViewLayout: layout)
self.whiteView.addSubview(collectionView)
collectionView.backgroundColor = UIColor.white
// MARK: - 注冊collectionViewCell
collectionView.register(HotCollectionViewCell.self, forCellWithReuseIdentifier: "HotCellColl")
collectionView.isScrollEnabled = false
collectionView.dataSource = self
collectionView.delegate = self
}
}
extension HotCell {
func collectionView(_ collectionView: UICollectionView,
numberOfItemsInSection section: Int) -> Int {
return self.preDrawCodeArr?.count ?? 0
}
}
// 解決UICollectionViewCell/UITableViewCell因重用機制導致的錯亂問題
func collectionView(_ collectionView: UICollectionView,
cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
// print("***",self.lotCode.description,"***")
// MARK: Method 1 - 代碼顯示完全正確
/*
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "HotCellColl",for: indexPath) as! LotteryHotCollectionViewCell
cell.preDrawCode.text = self.preDrawCodeArr![indexPath.item] as? String
return cell
*/
// FIXME: Method 2 - 圖片待修復! 06.10 修復
let identifier = "HotCellColl"
let cell: HotCollectionViewCell? = collectionView.dequeueReusableCell(withReuseIdentifier: identifier, for: indexPath) as? HotCollectionViewCell
// 先移除舊的
for subView: UIView in cell!.subviews {
if subView.tag == 20001 || subView.tag == 20002 {
subView.removeFromSuperview()
}
}
// FIXME:加入新的 ,這個非常關鍵
cell?.addButton()
// 顯示
switch self.lotCode {
case 10001,10037:
cell?.preDrawCodeImage?.image = UIImage(named: "J\(self.preDrawCodeArr![indexPath.item])")
case 10009:
cell?.preDrawCodeImage?.image = UIImage(named: "M\(self.preDrawCodeArr![indexPath.item])")
case 10007:
cell?.preDrawCodeImage?.image = UIImage(named: "D\(self.preDrawCodeArr![indexPath.item])")
default:
cell?.preDrawCode.text = self.preDrawCodeArr![indexPath.item] as? String
}
return cell!
}
(三)、collectionViewCell部份
override init(frame: CGRect) {
super.init(frame: frame)
addButton()
}
// MARK: - 這個方法要 暴露出來 給重用CELL時的方法 以供使用 by apiapia ON 06.10.2017
func addButton(){
getPreDrawCode()
getPreDrawCodeImage()
}
func getPreDrawCode(){
preDrawCode = UILabel.init(frame: CGRect(x: 1, y: 1, width: 25, height: 25))
preDrawCode.tag = 20002
self.preDrawCode.font = UIFont.systemFont(ofSize: 13)
self.preDrawCode.textColor = UIColor.white
self.preDrawCode.backgroundColor = UIColor(red: 45, green: 128, blue: 206)
self.preDrawCode.textAlignment = NSTextAlignment.center
self.preDrawCode.layer.cornerRadius = self.preDrawCode.size.width/2
self.preDrawCode.layer.masksToBounds = true
self.addSubview(preDrawCode)
}
寫在最后的話,因為,項目為公司項目,無法貼出所有代碼,只能上關鍵部份代碼,但這幾個已經可以解決表格重用CELL重疊或者CELL個數不一致的問題了,希望對你有用_
-- MARK by apiapia chan on 06.10.2017