給 UITableView 貼上好瓷磚
本文導航:
-1.隱藏分割線
-2.隱藏多余Cell
-3.分割線頭部頂?shù)降?、分割線顏色
-4.自定義點擊后效果 Cell 背景等更改
-5.類似button點擊效果 Cell - 閃一下
-6.Tableview視圖Cell進入動畫 從底部往上彈
-7.TableviewCell 使用SB約束 根據(jù)大小自動布局
-8. Cell 點擊展開
-附加-沒有數(shù)據(jù)時候提示
# 1.隱藏分割線
# 2.隱藏多余Cell
//##?在ViewController初始化時候加載 如viewDidLoad
//隱藏分割線
tableView.separatorStyle = UITableViewCellSeparatorStyle.None
//隱藏多余的cell
tableView.tableFooterView = UIView(frame: CGRectZero)
# 3.分割線頭部頂?shù)降?、分割線顏色
///##?分割線頭部頂?shù)降?、分割線顏色
//啟動、旋轉(zhuǎn)、視圖大小位置發(fā)生改變、增加子視圖等..都會調(diào)用
override func viewDidLayoutSubviews() {
tableView.separatorInset = UIEdgeInsetsZero
tableView.layoutMargins = UIEdgeInsetsZero
//articleTableView.separatorColor = UIColor.redColor() //分割線顏色
}
//沒當cell即將出現(xiàn)屏幕時候都會調(diào)用此方法
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
cell.separatorInset = UIEdgeInsetsZero
cell.layoutMargins = UIEdgeInsetsZero
}
# 4.自定義點擊后效果 Cell 背景等更改
//##?在cellForRowAtIndexPath方法使用
//點擊Cell時,沒有點擊效果
cell.selectionStyle = UITableViewCellSelectionStyle.None
//系統(tǒng)默認的顏色 .Blue藍色-默認 .Grap灰色 .None 無色
//點擊Cell時,自定義選中后的背景視圖
//背景顏色
cell.selectedBackgroundView = UIView()
cell.selectedBackgroundView?.backgroundColor = UIColor.clearColor()
//背景圖片
cell.selectedBackgroundView = UIImageView(image: UIImage(named: article.avatarImage))
//cell 右邊的輔助的提示
cell.accessoryType = .DisclosureIndicator //>
//.Checkmark //√ .DetailDisclosureButton // ! > .DetailButton // !
# 5.類似button點擊效果 Cell - 閃一下
//##?在 didSelectRowAtIndexPath 方法內(nèi)使用
//點擊Cell時 一閃而過 適合轉(zhuǎn)場時候交互 -
tableView.deselectRowAtIndexPath(indexPath, animated: false) // - true 動畫慢吞吞,適合不轉(zhuǎn)場時

Cell進入動畫
# 6.Tableview視圖Cell進入動畫 從底部往上彈
//##?加載動畫 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自動布局
# 7.TableviewCell 使用SB約束好, 根據(jù)大小自動布局
///使用SB布局的Cell ,直接使用下面代碼達到自動布局目的
//##?在ViewController初始化時候加載 如viewDidLoad
tableView.estimatedRowHeight = 44
tableView.rowHeight = UITableViewAutomaticDimension
//詳情可以參考這位兄弟的簡文 -- http://m.itdecent.cn/p/405391bfc2f2

8.cell 點擊展開
# 8.cell 點擊展開
//比如一個使用了SB約束好的label ,tag = 666 把他 屬性 lines = 0 與 1轉(zhuǎn)換 即顯示單行或多行
// -1.記得使用SB設置好約束
override func viewDidLoad() {
super.viewDidLoad()
// 0.啟動自動布局計劃
tableView.estimatedRowHeight = 44
tableView.rowHeight = UITableViewAutomaticDimension
}
// 1.先聲明的一個字典 - 記錄每個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 = "本文導航 \n 1.隱藏分割線\n 2.隱藏多余Cell\n 3.分割線頭部頂?shù)降住⒎指罹€顏色\n 4.自定義點擊后效果 Cell 背景等更改\n 5.類似button點擊效果 Cell - 閃一下\n 6.Tableview視圖Cell進入動畫 從底部往上彈\n 7.TableviewCell使用SB約束 自動布局 \n 8. cell 點擊展開"
if dict[indexPath.row] == false {
label.numberOfLines = 0
} else {
label.numberOfLines = 1
}
return cell
}
// 3. 在 beginUpdates() - endUpdates() 放代碼 有連續(xù)動畫效果
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated: true) //點擊閃動效果
let cell = tableView.cellForRowAtIndexPath(indexPath)
let label = cell!.contentView.viewWithTag(666) as! UILabel
tableView.beginUpdates() //開始
if label.numberOfLines == 0 {
label.numberOfLines = 1
dict[indexPath.row] = true
} else {
label.numberOfLines = 0
dict[indexPath.row] = false
}
tableView.endUpdates()
}

加載失敗
##-附加-沒有數(shù)據(jù)時候提示 可以自行加入空數(shù)據(jù)時候顯示
//判斷有沒有數(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)
}
}