引言:
系统内置的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