iOS - Tabview 分成不同 Group

當我們想將 TabVIew 分為2個 Group 時, 如 已搜尋到的藍芽裝置和已連接上的藍芽裝置, 可利用 Header 或 Footer來分隔.

Step 1: 在主畫面中拉進TableView , 並在 TableView中拉進 TableView Cell


Step 2: 繼承 UITableViewDataSource Protocol 並實做

群組個數
由於目前我們要分成2個Group, 所以設置為兩個sections

1
2
3
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 2;
}

群組名稱
此範例利用 Header來設定名稱

1
2
3
4
5
6
7
8
9
10
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
switch (section) {
case 0:
return "Found Devices:";
case 1:
return "Connected Devices:";
default:
return "Unknow";
}
}

各群組資料數量, 在此我們先設定為0

1
2
3
4
5
6
7
8
9
10
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
switch (section) {
case 0:
return 0;
case 1:
return 0;
default:
return 0;
}
}

設定TableViewCell
在.storyboard中, 設定參考 Cell 名稱

1
2
3
4
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell: UITableViewCell = UITableViewCell( style:UITableViewCellStyle.Subtitle, reuseIdentifier: "deviceCell");
return cell;
}
作者

Nick Lin

發表於

2016-07-07

更新於

2023-01-18

許可協議


評論