給 UITableView 貼上好瓷磚
本文導(dǎo)航:
-1.隱藏分割線
-2.隱藏多余Cell
-3.分割線頭部頂?shù)降住⒎指罹€顏色
-4.自定義點(diǎn)擊后效果 Cell 背景等更改
-5.類(lèi)似button點(diǎn)擊效果 Cell - 閃一下
-6.Tableview視圖Cell進(jìn)入動(dòng)畫(huà) 從底部往上彈
-7.TableviewCell 使用SB約束 根據(jù)大小自動(dòng)布局
-8. Cell 點(diǎn)擊展開(kāi)
-附加-沒(méi)有數(shù)據(jù)時(shí)候提示
# 1.隱藏分割線
# 2.隱藏多余Cell
//##?在ViewController初始化時(shí)候加載 如viewDidLoad
//隱藏分割線
tableView.separatorStyle = UITableViewCellSeparatorStyle.None
//隱藏多余的cell
tableView.tableFooterView = UIView(frame: CGRectZero)
# 3.分割線頭部頂?shù)降住⒎指罹€顏色
///##?分割線頭部頂?shù)降住⒎指罹€顏色
//啟動(dòng)、旋轉(zhuǎn)、視圖大小位置發(fā)生改變、增加子視圖等..都會(huì)調(diào)用
override func viewDidLayoutSubviews() {
tableView.separatorInset = UIEdgeInsetsZero
tableView.layoutMargins = UIEdgeInsetsZero
//articleTableView.separatorColor = UIColor.redColor() //分割線顏色
}
//沒(méi)當(dāng)cell即將出現(xiàn)屏幕時(shí)候都會(huì)調(diào)用此方法
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
cell.separatorInset = UIEdgeInsetsZero
cell.layoutMargins = UIEdgeInsetsZero
}
# 4.自定義點(diǎn)擊后效果 Cell 背景等更改
//##?在cellForRowAtIndexPath方法使用
//點(diǎn)擊Cell時(shí),沒(méi)有點(diǎn)擊效果
cell.selectionStyle = UITableViewCellSelectionStyle.None
//系統(tǒng)默認(rèn)的顏色 .Blue藍(lán)色-默認(rèn) .Grap灰色 .None 無(wú)色
//點(diǎn)擊Cell時(shí),自定義選中后的背景視圖
//背景顏色
cell.selectedBackgroundView = UIView()
cell.selectedBackgroundView?.backgroundColor = UIColor.clearColor()
//背景圖片
cell.selectedBackgroundView = UIImageView(image: UIImage(named: article.avatarImage))
//cell 右邊的輔助的提示
cell.accessoryType = .DisclosureIndicator //>
//.Checkmark //√ .DetailDisclosureButton // ! > .DetailButton // !
# 5.類(lèi)似button點(diǎn)擊效果 Cell - 閃一下
//##?在 didSelectRowAtIndexPath 方法內(nèi)使用
//點(diǎn)擊Cell時(shí) 一閃而過(guò) 適合轉(zhuǎn)場(chǎng)時(shí)候交互 -
tableView.deselectRowAtIndexPath(indexPath, animated: false) // - true 動(dòng)畫(huà)慢吞吞,適合不轉(zhuǎn)場(chǎng)時(shí)
Cell進(jìn)入動(dòng)畫(huà)
# 6.Tableview視圖Cell進(jìn)入動(dòng)畫(huà) 從底部往上彈
//##?加載動(dòng)畫(huà) Cell 往上沖 在 viewWillAppear 中使用
override func viewWillAppear(animated: Bool) {
animateTable()
}
func animateTable() {
self.tableView.reloadData()
let cells = tableView.visibleCells
let tableHeight = tableView.bounds.size.height
cells.forEach {
$0.transform = CGAffineTransformMakeTranslation(0, tableHeight)
}
var index = 0
cells.forEach {
let cell = $0
UIView.animateWithDuration(1.0, delay: 0.05 * Double(index), usingSpringWithDamping: 0.8, initialSpringVelocity: 0, options: [], animations: {
cell.transform = CGAffineTransformMakeTranslation(0, 0)
}, completion: nil)
index += 1
}
}
SB自動(dòng)布局
# 7.TableviewCell 使用SB約束好, 根據(jù)大小自動(dòng)布局
///使用SB布局的Cell ,直接使用下面代碼達(dá)到自動(dòng)布局目的
//##?在ViewController初始化時(shí)候加載 如viewDidLoad
tableView.estimatedRowHeight = 44
tableView.rowHeight = UITableViewAutomaticDimension
//詳情可以參考這位兄弟的簡(jiǎn)文 -- http://www.lxweimin.com/p/405391bfc2f2
8.cell 點(diǎn)擊展開(kāi)
# 8.cell 點(diǎn)擊展開(kāi)
//比如一個(gè)使用了SB約束好的label ,tag = 666 把他 屬性 lines = 0 與 1轉(zhuǎn)換 即顯示單行或多行
// -1.記得使用SB設(shè)置好約束
override func viewDidLoad() {
super.viewDidLoad()
// 0.啟動(dòng)自動(dòng)布局計(jì)劃
tableView.estimatedRowHeight = 44
tableView.rowHeight = UITableViewAutomaticDimension
}
// 1.先聲明的一個(gè)字典 - 記錄每個(gè)cell展收狀態(tài)
var dict:Dictionary<Int,Bool> = [:]
// 2.根據(jù)字典顯示cell狀態(tài)
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
let label = cell.contentView.viewWithTag(666) as! UILabel
label.text = "本文導(dǎo)航 \n 1.隱藏分割線\n 2.隱藏多余Cell\n 3.分割線頭部頂?shù)降住⒎指罹€顏色\n 4.自定義點(diǎn)擊后效果 Cell 背景等更改\n 5.類(lèi)似button點(diǎn)擊效果 Cell - 閃一下\n 6.Tableview視圖Cell進(jìn)入動(dòng)畫(huà) 從底部往上彈\n 7.TableviewCell使用SB約束 自動(dòng)布局 \n 8. cell 點(diǎn)擊展開(kāi)"
if dict[indexPath.row] == false {
label.numberOfLines = 0
} else {
label.numberOfLines = 1
}
return cell
}
// 3. 在 beginUpdates() - endUpdates() 放代碼 有連續(xù)動(dòng)畫(huà)效果
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated: true) //點(diǎn)擊閃動(dòng)效果
let cell = tableView.cellForRowAtIndexPath(indexPath)
let label = cell!.contentView.viewWithTag(666) as! UILabel
tableView.beginUpdates() //開(kāi)始
if label.numberOfLines == 0 {
label.numberOfLines = 1
dict[indexPath.row] = true
} else {
label.numberOfLines = 0
dict[indexPath.row] = false
}
tableView.endUpdates()
}
加載失敗
##-附加-沒(méi)有數(shù)據(jù)時(shí)候提示 可以自行加入空數(shù)據(jù)時(shí)候顯示
//判斷有沒(méi)有數(shù)據(jù)顯示 提示
func showIfNoAnswer() {
let imageView = UIImageView(frame: CGRectMake(0, 0, 60, 60))
let image = UIImage(named: "sad")
imageView.image = image?.imageWithRenderingMode(.AlwaysTemplate)
imageView.tintColor = UIColor.grayColor()
imageView.center = CGPointMake(self.view.center.x, 145)
imageView.tag = 33 // 方便 remove
self.view.addSubview(imageView)
let label = UILabel(frame: .zero)
label.text = "加載失敗"
label.font = UIFont(name: "New Gulim", size: 20)
label.textColor = UIColor.grayColor()
label.textAlignment = .Center
label.tag = 3
label.sizeToFit()
label.backgroundColor = UIColor.clearColor()
label.center = CGPointMake(self.view.center.x, 200)
view.addSubview(label)
}
}