示例图
none模式

disclosureIndicator

UITableViewController根视图控制器实现表视图
步骤
-
创建一个iOS工程
删除storyboard中View Controller Scene 中的View Controller,再从对象库拖入一个Table View Controller到设计界面
打开Table View Controller属性检查器,勾选Is Initial View Controller选项,否则应用启动后是黑屏
将ViewController类的父类由UIViewController改为UITableViewController
打开View Controller的属性选择器在Class列表中选择ViewController
UITableViewController默认以注册UITableViewDataSource和UITableViewDelegate协议,不需要再注册
实现
import UIKit
class ViewController: UITableViewController {
//全部数据
var listItems: NSArray!
override func viewDidLoad() {
super.viewDidLoad()
//读取资源文件数据
let listPath = Bundle.main.path(forResource: "team", ofType: "plist")
self.listItems = NSArray(contentsOfFile: listPath!)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
//返回列表每行的视图
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CustomId", for: indexPath)
let row = indexPath.row
let rowDict = self.listItems[row] as! NSDictionary
cell.textLabel?.text = rowDict["name"] as? String
cell.detailTextLabel?.text = "123"
let imagePath = String(format: "%@.png", rowDict["image"] as! String)
cell.imageView?.image = UIImage(named: imagePath)
cell.accessoryType = UITableViewCellAccessoryType.disclosureIndicator
return cell
}
//返回条目数目
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.listItems.count
}
//响应条目点击事件
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("点击事件")
}
}
示例图
detailButton模式

checkmark模式

自定义单元格
步骤
-
创建一个表视图工程
修改根视图控制器为表视图控制器UITableViewController,参照上节的步骤
从对象库中拖入控件到单元格内部,比如Lable和ImageView
创建自定义单元格类CustomCell文件,并继承UITableViewCell类
在设计界面中选择View Controller Scene中的Table View Cell,并打开属性检查器,将Class设为CustomCell类,并设置单元格的Identifier
为单元格中的控件Label和ImageView控件连接输出接口,将控件绑定到CustomCell类中
打开ViewController类,编写代码实现










