先在viewDidLoad()中建立UITableView :
设置原点以及尺寸
let myTableView = UITableView(frame: CGRect(x: 0, y: 20,
width: fullScreenSize.width,
height: fullScreenSize.height - 20), style: .grouped)
注册cell的样式即名称
myTableView.register(UITableViewCell.self,
forCellReuseIdentifier: "Cell")
myTableView.register(CollectionCell.self,
forCellReuseIdentifier: "Collection") //
Quick Help中register

重新添加cell的时候,这里编译时报错
因为class的类定义错了 原本想要新定义的新的类,定义至了子类中
修改过后通过编译的版本

人蠢就要多读书.......
其他设置
// 设置委任对象
myTableView.delegate = self
myTableView.dataSource = self
// 分割线的样式
myTableView.separatorStyle = .singleLine
// 分割线的距离 四个数值分別代表 上、左、下、右 的間距
myTableView.separatorInset =
UIEdgeInsetsMake(0, 20, 0, 20)
// 是否可以点选 cell
myTableView.allowsSelection = true
// 是否可以多选 cell
myTableView.allowsMultipleSelection = false
// 加入到页面中
self.view.addSubview(myTableView)
tableView中必须定义的两个内容,包括:
定义一组有几个cell 以及每个cell要展示的内容 定义每一组有几个cell
func tableView(_ tableView:
UITableView, numberOfRowsInSection section: Int) -> Int {
return info[section].count
}
其中info是定义的数组var变量
var info = [
["林書豪","陳信安"],
["陳偉殷","王建民","陳金鋒","林智勝"]
]
每个cell要展示的内容
func tableView(_ tableView:
UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// 取得 tableView 目前使用的 cell
let cell:UITableViewCell!
if indexPath.section == 0 && indexPath.row == 0 {
let collectionCell = tableView.dequeueReusableCell(withIdentifier: "Collection", for: indexPath) as! CollectionCell
collectionCell.setUp()
collectionCell.cTextLabel.text = "hello"
cell = collectionCell
}else{
cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! UITableViewCell

定义的新的cell

Quick Help中dequeueReusableCell的解释

在再次编译中dequeueReusableCell的编译报错
因为在此之前class的定义出错了

在整个函数中继续设置Accessory 按钮样式等
// 必须实作的方法:每一組有几个 cell
func tableView(tableView: UITableView,
numberOfRowsInSection section: Int) -> Int {
return info[section].count
}
// 必須实作的方法:每個 cell 要展示的內容
func tableView(tableView: UITableView,
cellForRowAtIndexPath indexPath: NSIndexPath)
-> UITableViewCell {
// 取得 tableView 目前使用的 cell
let cell =
tableView.dequeueReusableCellWithIdentifier(
"Cell", forIndexPath: indexPath) as
UITableViewCell
// 设置Accessory 按钮样式
if indexPath.section == 1 {
if indexPath.row == 0 {
cell.accessoryType = .Checkmark
} else if indexPath.row == 1 {
cell.accessoryType = .DetailButton
} else if indexPath.row == 2 {
cell.accessoryType =
.DetailDisclosureButton
} else if indexPath.row == 3 {
cell.accessoryType = .DisclosureIndicator
}
}
// 展示的內容
if let myLabel = cell.textLabel {
myLabel.text =
"\(info[indexPath.section][indexPath.row])"
}
return cell
}
以及执行的动作
// 點選 cell 後执行的动作
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
// 取消 cell 的選取狀態
tableView.deselectRow(at: indexPath, animated: true)
let name = info[indexPath.section][indexPath.row]
print("選擇的是 \(name)")
}
// 點選 Accessory 按钮执行的动作
// 必須設置 cell 的 accessoryType
func tableView(_ tableView: UITableView, accessoryButtonTappedForRowWith indexPath: IndexPath) {
let name = info[indexPath.section][indexPath.row]
print("按下的是 \(name) 的 detail")
}
// 有几组 section
func numberOfSections(in tableView: UITableView) -> Int {
return info.count
}
// 每个 section 的标题
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
let title = section == 0 ? "籃球" : "棒球"
return title
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 200
}
// 设定宽度