日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

uiswitch样式_Swift - 表格UITableView的plain、grouped两种样式详解(附分组头悬停)

發(fā)布時(shí)間:2023/12/15 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 uiswitch样式_Swift - 表格UITableView的plain、grouped两种样式详解(附分组头悬停) 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

在表格 tableview初始化的時(shí)候我們可以指定需要使用的 UITableViewStyle樣式,可用的樣式一共有兩種:.plain和 .grouped。下面分別對(duì)它們做介紹。

一、plain模式

1,默認(rèn)樣式

在 plain模式下,如果 tableview有多個(gè) section(分區(qū)、分組),組與組之間默認(rèn)是沒(méi)有間距的。

同時(shí)組頭或組尾會(huì)有 sticky效果(粘性效果、懸停效果),即表格滾動(dòng)時(shí)組頭與組尾會(huì)自動(dòng)停留,而不是跟隨單元格一同移動(dòng)。

? ? ??

import UIKit

class ViewController: UIViewController , UITableViewDelegate, UITableViewDataSource{

var tableView:UITableView?

//分組頭標(biāo)題

var articleHeaders:[String]!

//所有文章標(biāo)題

var articleNames:Dictionary!

override func loadView() {

super.loadView()

}

override func viewDidLoad() {

super.viewDidLoad()

//初始化數(shù)據(jù)

self.articleNames = [

0:[String]([

"1、文本標(biāo)簽(UILabel)的用法",

"2、按鈕(UIButton)的用法",

"3、文本輸入框(UITextField)的用法",

"4、多行文本輸入框(UITextView)的用法",

"5、開(kāi)關(guān)按鈕(UISwitch)的用法",

"6、分段選擇控件(UISegmentedControl)的用法",

"7、圖像控件(UIImageView)的用法",

]),

1:[String]([

"1、使用占位符文本placeholder添加文本框提示",

"2、使用autofocus讓控件自動(dòng)獲取焦點(diǎn)",

"3、表單客戶(hù)端驗(yàn)證",

"4、日期和時(shí)間選擇輸入",

"5、顏色選擇器",])

]

self.articleHeaders = [

"Swift文章",

"HTML5文章"

]

//創(chuàng)建表視圖

self.tableView = UITableView(frame:self.view.frame, style:.plain)

self.tableView!.delegate = self

self.tableView!.dataSource = self

//創(chuàng)建一個(gè)重用的單元格

self.tableView!.register(UITableViewCell.self,

forCellReuseIdentifier: "SwiftCell")

self.view.addSubview(self.tableView!)

}

//在本例中,有2個(gè)分區(qū)

func numberOfSections(in tableView: UITableView) -> Int {

return self.articleHeaders.count

}

//返回表格行數(shù)(也就是返回控件數(shù))

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

let data = self.articleNames[section]

return data!.count

}

// UITableViewDataSource協(xié)議中的方法,該方法的返回值決定指定分區(qū)的頭部

func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int)

-> String? {

return self.articleHeaders[section]

}

// UITableViewDataSource協(xié)議中的方法,該方法的返回值決定指定分區(qū)的尾部

func tableView(_ tableView:UITableView, titleForFooterInSection section:Int)->String? {

let data = self.articleNames[section]

return "有\(zhòng)(data!.count)篇文章"

}

//創(chuàng)建各單元顯示內(nèi)容(創(chuàng)建參數(shù)indexPath指定的單元)

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath)

-> UITableViewCell {

//為了提供表格顯示性能,已創(chuàng)建完成的單元需重復(fù)使用

let identify:String = "SwiftCell"

//同一形式的單元格重復(fù)使用,在聲明時(shí)已注冊(cè)

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,調(diào)整分組間的間距

如果需要設(shè)置組與組之間的間距,可以通過(guò) viewForHeaderInSection、viewForFooterInSection、heightForHeaderInSection或 heightForFooterInSection這幾個(gè)方法配合實(shí)現(xiàn)。

3,去除分組頭、分組尾的停留效果

這個(gè)通過(guò)重寫(xiě) tableView的?scrollViewDidScroll方法可以實(shí)現(xiàn)。要注意的是頁(yè)面是否有導(dǎo)航控制器,有的話要把自動(dòng)內(nèi)邊距調(diào)整給考慮進(jìn)去。

(1)分組頭部不懸停

//header不懸停

func scrollViewDidScroll(_ scrollView: UIScrollView) {

//組頭高度

let sectionHeaderHeight:CGFloat = 30

//獲取是否有默認(rèn)調(diào)整的內(nèi)邊距

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

//獲取是否有默認(rèn)調(diào)整的內(nèi)邊距

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

//獲取是否有默認(rèn)調(diào)整的內(nèi)邊距

let defaultEdgeTop:CGFloat = navigationController?.navigationBar != nil

&& self.automaticallyAdjustsScrollViewInsets ? 64 : 0

//上邊距相關(guān)

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

}

//下邊距相關(guān)

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

}

//設(shè)置內(nèi)邊距

scrollView.contentInset = UIEdgeInsetsMake(edgeTop, 0, edgeBottom, 0)

}

二、grouped模式

1,默認(rèn)樣式

在 grouped模式下,如果 tableview有多個(gè) section(分區(qū)、分組),組與組之間默認(rèn)是有間距的。

而且在表格滾動(dòng)的同時(shí)組頭與組尾會(huì)隨之滾動(dòng)、不停留,不會(huì)有 sticky效果(粘性效果、懸停效果)。

下面分別是:分區(qū)頭尾均有、只有分區(qū)頭、分區(qū)頭尾都沒(méi)有。這三種情況:

? ? ??

? ? ??

2,去掉多余的間距

(1)在分組頭、分組尾都存在時(shí),可以將 tableview最上方的間距給去除。

//去除表格上放多余的空隙

self.tableView?.contentInset = UIEdgeInsetsMake(-20, 0, 0, 0)

(2)如果只有分組頭,沒(méi)有分組尾,除了將 tableview最上方的間距給去除,還可以將分組尾的高度設(shè)置為 0.01(不能設(shè)為 0,否則無(wú)效)。同時(shí)還要將分組尾設(shè)置成一個(gè)空的 UIView(否則在 iOS11 下分組尾高度不會(huì)起作用)。

//去除表格上放多余的空隙

self.tableView?.contentInset = UIEdgeInsetsMake(-20, 0, 0, 0)

//設(shè)置分組尾的高度

func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {

return 0.01

}

//將分組尾設(shè)置為一個(gè)空的View

func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {

return UIView()

}

(3)如果分組頭、分組尾均沒(méi)有,還可以將分組頭的高度設(shè)置為 0.01。

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {

return 0.01

}

總結(jié)

以上是生活随笔為你收集整理的uiswitch样式_Swift - 表格UITableView的plain、grouped两种样式详解(附分组头悬停)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。