uiswitch样式_Swift - 表格UITableView的plain、grouped两种样式详解(附分组头悬停)
在表格 tableview初始化的時候我們可以指定需要使用的 UITableViewStyle樣式,可用的樣式一共有兩種:.plain和 .grouped。下面分別對它們做介紹。
一、plain模式
1,默認樣式
在 plain模式下,如果 tableview有多個 section(分區、分組),組與組之間默認是沒有間距的。
同時組頭或組尾會有 sticky效果(粘性效果、懸停效果),即表格滾動時組頭與組尾會自動停留,而不是跟隨單元格一同移動。
? ? ??
import UIKit
class ViewController: UIViewController , UITableViewDelegate, UITableViewDataSource{
var tableView:UITableView?
//分組頭標題
var articleHeaders:[String]!
//所有文章標題
var articleNames:Dictionary!
override func loadView() {
super.loadView()
}
override func viewDidLoad() {
super.viewDidLoad()
//初始化數據
self.articleNames = [
0:[String]([
"1、文本標簽(UILabel)的用法",
"2、按鈕(UIButton)的用法",
"3、文本輸入框(UITextField)的用法",
"4、多行文本輸入框(UITextView)的用法",
"5、開關按鈕(UISwitch)的用法",
"6、分段選擇控件(UISegmentedControl)的用法",
"7、圖像控件(UIImageView)的用法",
]),
1:[String]([
"1、使用占位符文本placeholder添加文本框提示",
"2、使用autofocus讓控件自動獲取焦點",
"3、表單客戶端驗證",
"4、日期和時間選擇輸入",
"5、顏色選擇器",])
]
self.articleHeaders = [
"Swift文章",
"HTML5文章"
]
//創建表視圖
self.tableView = UITableView(frame:self.view.frame, style:.plain)
self.tableView!.delegate = self
self.tableView!.dataSource = self
//創建一個重用的單元格
self.tableView!.register(UITableViewCell.self,
forCellReuseIdentifier: "SwiftCell")
self.view.addSubview(self.tableView!)
}
//在本例中,有2個分區
func numberOfSections(in tableView: UITableView) -> Int {
return self.articleHeaders.count
}
//返回表格行數(也就是返回控件數)
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let data = self.articleNames[section]
return data!.count
}
// UITableViewDataSource協議中的方法,該方法的返回值決定指定分區的頭部
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int)
-> String? {
return self.articleHeaders[section]
}
// UITableViewDataSource協議中的方法,該方法的返回值決定指定分區的尾部
func tableView(_ tableView:UITableView, titleForFooterInSection section:Int)->String? {
let data = self.articleNames[section]
return "有\(data!.count)篇文章"
}
//創建各單元顯示內容(創建參數indexPath指定的單元)
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath)
-> UITableViewCell {
//為了提供表格顯示性能,已創建完成的單元需重復使用
let identify:String = "SwiftCell"
//同一形式的單元格重復使用,在聲明時已注冊
let cell = tableView.dequeueReusableCell(withIdentifier: identify,
for: indexPath)
cell.accessoryType = .disclosureIndicator
var data = self.articleNames[indexPath.section]
cell.textLabel?.text = data![indexPath.row]
return cell
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
2,調整分組間的間距
如果需要設置組與組之間的間距,可以通過 viewForHeaderInSection、viewForFooterInSection、heightForHeaderInSection或 heightForFooterInSection這幾個方法配合實現。
3,去除分組頭、分組尾的停留效果
這個通過重寫 tableView的?scrollViewDidScroll方法可以實現。要注意的是頁面是否有導航控制器,有的話要把自動內邊距調整給考慮進去。
(1)分組頭部不懸停
//header不懸停
func scrollViewDidScroll(_ scrollView: UIScrollView) {
//組頭高度
let sectionHeaderHeight:CGFloat = 30
//獲取是否有默認調整的內邊距
let defaultEdgeTop:CGFloat = navigationController?.navigationBar != nil
&& self.automaticallyAdjustsScrollViewInsets ? 64 : 0
if scrollView.contentOffset.y >= -defaultEdgeTop &&
scrollView.contentOffset.y <= sectionHeaderHeight - defaultEdgeTop {
scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0)
}
else if (scrollView.contentOffset.y>=sectionHeaderHeight - defaultEdgeTop) {
scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight + defaultEdgeTop,
0, 0, 0)
}
}
(2)分組尾部不懸停
//footer不懸停
func scrollViewDidScroll(_ scrollView: UIScrollView) {
//組尾高度
let sectionFooterHeight:CGFloat = 30
//獲取是否有默認調整的內邊距
let defaultEdgeTop:CGFloat = navigationController?.navigationBar != nil
&& self.automaticallyAdjustsScrollViewInsets ? 64 : 0
let b = scrollView.contentOffset.y + scrollView.frame.height
let h = scrollView.contentSize.height - sectionFooterHeight
if b <= h {
scrollView.contentInset = UIEdgeInsetsMake(defaultEdgeTop, 0, -30, 0)
}else if b > h && b < scrollView.contentSize.height {
scrollView.contentInset = UIEdgeInsetsMake(defaultEdgeTop, 0, b - h - 30, 0)
}
}
(3)分組頭部、尾部均不懸停
//header、footer均不懸停
func scrollViewDidScroll(_ scrollView: UIScrollView) {
//組頭高度
let sectionHeaderHeight:CGFloat = 30
//組尾高度
let sectionFooterHeight:CGFloat = 30
//獲取是否有默認調整的內邊距
let defaultEdgeTop:CGFloat = navigationController?.navigationBar != nil
&& self.automaticallyAdjustsScrollViewInsets ? 64 : 0
//上邊距相關
var edgeTop = defaultEdgeTop
if scrollView.contentOffset.y >= -defaultEdgeTop &&
scrollView.contentOffset.y <= sectionHeaderHeight - defaultEdgeTop {
edgeTop = -scrollView.contentOffset.y
}
else if (scrollView.contentOffset.y>=sectionHeaderHeight - defaultEdgeTop) {
edgeTop = -sectionHeaderHeight + defaultEdgeTop
}
//下邊距相關
var edgeBottom:CGFloat = 0
let b = scrollView.contentOffset.y + scrollView.frame.height
let h = scrollView.contentSize.height - sectionFooterHeight
if b <= h {
edgeBottom = -30
}else if b > h && b < scrollView.contentSize.height {
edgeBottom = b - h - 30
}
//設置內邊距
scrollView.contentInset = UIEdgeInsetsMake(edgeTop, 0, edgeBottom, 0)
}
二、grouped模式
1,默認樣式
在 grouped模式下,如果 tableview有多個 section(分區、分組),組與組之間默認是有間距的。
而且在表格滾動的同時組頭與組尾會隨之滾動、不停留,不會有 sticky效果(粘性效果、懸停效果)。
下面分別是:分區頭尾均有、只有分區頭、分區頭尾都沒有。這三種情況:
? ? ??
? ? ??
2,去掉多余的間距
(1)在分組頭、分組尾都存在時,可以將 tableview最上方的間距給去除。
//去除表格上放多余的空隙
self.tableView?.contentInset = UIEdgeInsetsMake(-20, 0, 0, 0)
(2)如果只有分組頭,沒有分組尾,除了將 tableview最上方的間距給去除,還可以將分組尾的高度設置為 0.01(不能設為 0,否則無效)。同時還要將分組尾設置成一個空的 UIView(否則在 iOS11 下分組尾高度不會起作用)。
//去除表格上放多余的空隙
self.tableView?.contentInset = UIEdgeInsetsMake(-20, 0, 0, 0)
//設置分組尾的高度
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 0.01
}
//將分組尾設置為一個空的View
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
return UIView()
}
(3)如果分組頭、分組尾均沒有,還可以將分組頭的高度設置為 0.01。
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 0.01
}
總結
以上是生活随笔為你收集整理的uiswitch样式_Swift - 表格UITableView的plain、grouped两种样式详解(附分组头悬停)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 盯盘主要看什么 主要看以下四点
- 下一篇: 加杠杆和去杠杆的意思