隱藏分割線、隱藏多余Cell
//隱藏分割線
tableView.separatorStyle = UITableViewCellSeparatorStyle.None
//隱藏多余的cell
tableView.tableFooterView = UIView(frame: CGRectZero)
分割線頭部頂到底、分割線顏色
//啟動、旋轉、視圖大小位置發生改變、增加子視圖等都會調用
override func viewDidLayoutSubviews() {
tableView.separatorInset = UIEdgeInsetsZero
tableView.layoutMargins = UIEdgeInsetsZero
//articleTableView.separatorColor = UIColor.redColor() //分割線顏色
}
//沒當cell即將出現屏幕時候都會調用此方法
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
cell.separatorInset = UIEdgeInsetsZero
cell.layoutMargins = UIEdgeInsetsZero
}
點擊後效果 Cell 背景等更改
//點擊Cell時,沒有點擊效果
cell.selectionStyle = UITableViewCellSelectionStyle.None
//系統默認的顏色 .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 // !
類似button點擊效果閃一下
//在 didSelectRowAtIndexPath 方法內使用
//點擊Cell時 一閃而過 適合轉場時候交互 -
tableView.deselectRowAtIndexPath(indexPath, animated: false) // - true 動畫慢吞吞,適合不轉場時
Tableview視圖Cell進入動畫 從底部往上彈
//加載動畫 Cell 往上沖 在 viewWillAppear 中使用
func animateTable() {
self.tableView.reloadData()
let cells = tableView.visibleCells
let tableHeight = tableView.bounds.size.height
for i in cells {
let cell: UITableViewCell = i as UITableViewCell
cell.transform = CGAffineTransformMakeTranslation(0, tableHeight)
}
var index = 0
for a in cells {
let cell: UITableViewCell = a as UITableViewCell
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
}
}
點擊cell展開樣式
//比如一個使用了SB約束好的label ,tag = 666 把他 屬性 lines = 0 與 1轉換 即顯示單行或多行
// -1.記得使用SB設置好約束
override func viewDidLoad() {
super.viewDidLoad()
// 0.啟動自動布局計劃
tableView.estimatedRowHeight = 44
tableView.rowHeight = UITableViewAutomaticDimension
}
// 1.先聲明的一個字典 - 記錄每個cell展收狀態
var dict:Dictionary<Int,Bool> = [:]
// 2.根據字典顯示cell狀態
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.分割線頭部頂到底、分割線顏色\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() 放代碼 有連續動畫效果
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()
}
沒有數據時候提示 可以自行加入空數據時候顯示
//判斷有沒有數據顯示 提示
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)
}
}