1.需求場景
類似于靜態(tài)固定功能性cell + 下邊動(dòng)態(tài)數(shù)據(jù)展示性cell,如圖所示:
班級(jí)列表是動(dòng)態(tài)數(shù)量的cell
2.問題分析
眾所周知Storyboard支持TableViewController上的StaticCells類型,可實(shí)現(xiàn)上部功能性的cell,但下部的數(shù)量變化的cell就無法在該TableViewController上共存了
3.一種思路
注冊(cè)多個(gè)PrototypeCells璧眠,但對(duì)頂部和下部的cell進(jìn)行不同場景的復(fù)用,然后將復(fù)用的cell存儲(chǔ)成屬性,以模擬達(dá)到靜態(tài)的作用
4.具體實(shí)現(xiàn)
Storyboard布局
// 屬性
private var phoneCell: UITableViewCell!{
didSet{
// 做一些初始化設(shè)置
}
}
private var passwordCell: UITableViewCell!{
didSet{
// 做一些初始化設(shè)置
}
}
// cell 復(fù)用
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let section = indexPath.section
let row = indexPath.row
if section == 0 && row == 0{ //手機(jī)號(hào)
phoneCell = phoneCell ?? tableView.dequeueReusableCell(withIdentifier: "PhoneCell", for: indexPath)
return phoneCell
}
else if section == 0 && row == 1{ //密碼
passwordCell = passwordCell ?? tableView.dequeueReusableCell(withIdentifier: "PasswordCell", for: indexPath)
return passwordCell
}
// 班級(jí)
let cell = tableView.dequeueReusableCell(withIdentifier: "SchoolCell", for: indexPath)
return cell
}
5.最終效果
“靜態(tài)”cell表現(xiàn)良好责静,不受影響
6.源碼
https://github.com/BackWorld/HybridStaticPrototypeCellDemo
如果對(duì)你有幫助袁滥,別忘了給個(gè)??或點(diǎn)個(gè)??哦。