引言:
系统内置的cell样式由UITableViewCell类提供(该类由 NSClassFromString反射而来),在原型模型或小型系统而言开发便捷性有很大帮助,使用时指定init方法的参数即可
defalut样式:
样式说明:
一个ImageView在cell最左边;紧贴着该控件的是一个显示文字的Label,当然也可以添加辅助视图(这里为UISwitch)
图示:
代码:
class DFViewController: UIViewController, UITableViewDelegate, UITableViewDataSource{
var dataArray: Array<String> = ["C","C++","Java","Object-C","Swift", "C#"]
var tableView: UITableView!
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
dataArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCell(withIdentifier: "INTEGRAL_2", for: indexPath)
// cell样式
cell = UITableViewCell.init(style: UITableViewCell.CellStyle.default, reuseIdentifier: "INTEGRAL_2")
cell.textLabel?.text = dataArray[indexPath.row]
cell.imageView?.image = UIImage(named: "apple")
if (indexPath.row / 2) == 0 {
cell.detailTextLabel?.text = "Strong Static"
} else if (indexPath.row / 2) != 0{
cell.detailTextLabel?.text = "Weak Dynamic"
}
cell.accessoryView = UISwitch()
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.dequeueReusableCell(withIdentifier: "INTEGRAL_2", for: indexPath)
print("\(indexPath.row)")
switch indexPath.row {
case 2:
self.dismiss(animated: true, completion: nil)
default:
break
}
}
private func initView(){
tableView = UITableView(frame: self.view.frame, style: .plain)
tableView.register(NSClassFromString("UITableViewCell"), forCellReuseIdentifier: "INTEGRAL_2")
tableView.delegate = self
tableView.dataSource = self
self.view.addSubview(tableView)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.view.backgroundColor = UIColor.white
initView()
}
}
subtitle样式:
样式说明:
一个ImageView在cell最左边;紧贴着该控件的是一个显示主标题的Label,该Label下方还有一个字号较小的Label。其样式类似于微信
图示:
代码:
class SUBViewController: UIViewController, UITableViewDelegate, UITableViewDataSource{
var dataArray: Array<String> = ["Bob","Jackson","Edward","Lucy","Nancy", "Pony", "Leo", "Alice"]
var tableView: UITableView!
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
dataArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCell(withIdentifier: "SUB_CELL", for: indexPath)
cell = UITableViewCell.init(style: UITableViewCell.CellStyle.subtitle, reuseIdentifier: "SUB_CELL")
cell.textLabel?.text = dataArray[indexPath.row]
cell.imageView?.image = UIImage(named: "chat_user")
switch indexPath.row{
case 0:
cell.detailTextLabel?.text = "How long u will coming?"
case 1:
cell.detailTextLabel?.text = "[SMILE]"
case 2:
cell.detailTextLabel?.text = "Nice!! I'll do it now!"
case 3:
cell.detailTextLabel?.text = "OKay just to do as u said."
case 4:
cell.detailTextLabel?.text = "The work must be done after 1 hour."
case 5:
cell.detailTextLabel?.text = "U pay it."
case 6:
cell.detailTextLabel?.text = "[Received files...]"
case 7:
cell.detailTextLabel?.text = "[RedPackget]"
default:
cell.detailTextLabel?.text = "Jesus you forget that."
}
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("\(indexPath.row)")
switch indexPath.row {
case 2:
self.dismiss(animated: true, completion: nil)
default:
break
}
}
private func initView(){
tableView = UITableView(frame: self.view.frame, style: .plain)
tableView.register(NSClassFromString("UITableViewCell"), forCellReuseIdentifier: "SUB_CELL")
tableView.delegate = self
tableView.dataSource = self
self.view.addSubview(tableView)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.view.backgroundColor = UIColor.white
self.title = "We Chat(16)"
initView()
}
}
values1样式:
样式说明:
一个ImageView在cell最左边;紧贴着该控件的是一个显示主标题的Label;该cell最右方有一个用以显示副标题的Label。这里演示并没有显示ImageView
图示:
代码:
class V1ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource{
var dataArray: Array<String> = ["Deep Leaning","Maching Leaning","Computer Version","Natural Languague Process","Recommend"]
var tableView: UITableView!
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
dataArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCell(withIdentifier: "INTEGRAL_3", for: indexPath)
cell = UITableViewCell.init(style: UITableViewCell.CellStyle.value1, reuseIdentifier: "INTEGRAL_3")
cell.textLabel?.text = dataArray[indexPath.row]
cell.detailTextLabel?.text = "Click to browser."
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("\(indexPath.row)")
switch indexPath.row {
case 2:
self.dismiss(animated: true, completion: nil)
default:
break
}
}
private func initView(){
tableView = UITableView(frame: self.view.frame, style: .plain)
tableView.register(NSClassFromString("UITableViewCell"), forCellReuseIdentifier: "INTEGRAL_3")
tableView.delegate = self
tableView.dataSource = self
self.view.addSubview(tableView)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.view.backgroundColor = UIColor.white
initView()
}
}
values2样式:
样式说明:
主标题和副标题在cell内部0.618的位置
图示:
代码:
class V2ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource{
var dataArray: Array<String> = ["J2EE","EJB","Servlet","Spring","SpringBoot", "Dubbo"]
var tableView: UITableView!
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
dataArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCell(withIdentifier: "V2_CELL", for: indexPath)
cell = UITableViewCell.init(style: UITableViewCell.CellStyle.value2, reuseIdentifier: "V2_CELL")
cell.textLabel?.text = dataArray[indexPath.row]
cell.imageView?.image = UIImage(named: "apple")
cell.imageView?.isHidden = false
cell.detailTextLabel?.text = "Server"
cell.accessoryView = UISwitch()
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.dequeueReusableCell(withIdentifier: "V2_CELL", for: indexPath)
print("\(indexPath.row)")
if let lSwitch = cell.accessoryView as? UISwitch{
if lSwitch.isOn {
print("------> IS ON")
} else {
print("------> IS OFF")
}
}
switch indexPath.row {
case 2:
self.dismiss(animated: true, completion: nil)
default:
break
}
}
private func initView(){
tableView = UITableView(frame: self.view.frame, style: .plain)
tableView.register(NSClassFromString("UITableViewCell"), forCellReuseIdentifier: "V2_CELL")
tableView.delegate = self
tableView.dataSource = self
self.view.addSubview(tableView)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.view.backgroundColor = UIColor.white
initView()
}
}
动态使用:
动态使用要实现有关编辑样式、操作显示文本、操作事件的方法,其分别如下:
func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle
func tableView(_ tableView: UITableView, titleForDeleteConfirmationButtonForRowAt indexPath: IndexPath) -> String?
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath)
代码:
import UIKit
class DUViewController: UIViewController, UITableViewDelegate, UITableViewDataSource{
var dataArray: Array<String> = ["Swing","Winform","Coacoa","Qt","Xarmian", "Flutter"]
var tableView: UITableView!
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
dataArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCell(withIdentifier: "DU_CELL", for: indexPath)
cell.textLabel?.text = dataArray[indexPath.row]
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("\(indexPath.row)")
switch indexPath.row {
case 2:
self.dismiss(animated: true, completion: nil)
default:
break
}
}
func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle {
if indexPath.row == 0 {
return .insert
} else{
return .delete
}
}
func tableView(_ tableView: UITableView, titleForDeleteConfirmationButtonForRowAt indexPath: IndexPath) -> String? {
if indexPath.row == 0 {
return "Add"
} else{
return "Delete"
}
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
dataArray.remove(at: indexPath.row)
tableView.reloadData()
} else if editingStyle == .insert{
dataArray.append("Swift UI")
tableView.reloadData()
}
}
private func initView(){
tableView = UITableView(frame: self.view.frame, style: .plain)
tableView.register(NSClassFromString("UITableViewCell"), forCellReuseIdentifier: "DU_CELL")
tableView.isEditing = true
tableView.delegate = self
tableView.dataSource = self
self.view.addSubview(tableView)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.view.backgroundColor = UIColor.white
initView()
}
}
表头:
表头主要就是分区方法、
分区:
分区主要就是分区方法、表头、表尾方法了,其要实现以下方法:
func numberOfSections(in tableView: UITableView) -> Int
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String?
func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String?
代码如下:
class FGViewController: UIViewController, UITableViewDelegate, UITableViewDataSource{
var dataArray: Array<String> = ["Apple","Microsoft","Google","Amazon","Facebook", "ATT"]
var tableView: UITableView!
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
dataArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCell(withIdentifier: "FG_CELL", for: indexPath)
cell.textLabel?.text = dataArray[indexPath.row]
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("\(indexPath.row)")
switch indexPath.row {
case 2:
self.dismiss(animated: true, completion: nil)
default:
break
}
}
func numberOfSections(in tableView: UITableView) -> Int {
2
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
if(section == 0){
return "Header for 1 section"
} else{
return ""
}
}
func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? {
if(section == 0){
return "Footer for 1 section"
} else{
return "Footer for 2 section"
}
}
private func initView(){
tableView = UITableView(frame: self.view.frame, style: .plain)
tableView.register(NSClassFromString("UITableViewCell"), forCellReuseIdentifier: "FG_CELL")
tableView.delegate = self
tableView.dataSource = self
self.view.addSubview(tableView)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.view.backgroundColor = UIColor.white
initView()
}
}
Github地址:https://github.com/mcry416/integral_cell
转载:https://blog.csdn.net/kicinio/article/details/117196508
查看评论