iOS開發(fā) UITableView 常用細(xì)節(jié)

給 UITableView 貼上好瓷磚

本文導(dǎo)航:

-1.隱藏分割線
-2.隱藏多余Cell
-3.分割線頭部頂?shù)降住⒎指罹€顏色
-4.自定義點(diǎn)擊后效果 Cell 背景等更改
-5.類似button點(diǎn)擊效果 Cell - 閃一下
-6.Tableview視圖Cell進(jìn)入動(dòng)畫 從底部往上彈
-7.TableviewCell 使用SB約束 根據(jù)大小自動(dòng)布局
-8. Cell 點(diǎn)擊展開
-附加-沒有數(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() //分割線顏色
    }
    //沒當(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í),沒有點(diǎn)擊效果
  cell.selectionStyle = UITableViewCellSelectionStyle.None
  //系統(tǒng)默認(rèn)的顏色  .Blue藍(lán)色-默認(rèn) .Grap灰色 .None 無色

 //點(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.類似button點(diǎn)擊效果 Cell - 閃一下
   //##?在 didSelectRowAtIndexPath 方法內(nèi)使用
   //點(diǎn)擊Cell時(shí) 一閃而過 適合轉(zhuǎn)場(chǎng)時(shí)候交互 - 
  tableView.deselectRowAtIndexPath(indexPath, animated: false) // - true 動(dòng)畫慢吞吞,適合不轉(zhuǎn)場(chǎng)時(shí)

Cell進(jìn)入動(dòng)畫

# 6.Tableview視圖Cell進(jìn)入動(dòng)畫 從底部往上彈
//##?加載動(dòng)畫 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)擊展開

# 8.cell 點(diǎn)擊展開
//比如一個(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.類似button點(diǎn)擊效果 Cell - 閃一下\n 6.Tableview視圖Cell進(jìn)入動(dòng)畫 從底部往上彈\n 7.TableviewCell使用SB約束 自動(dòng)布局 \n 8. cell 點(diǎn)擊展開"
        if dict[indexPath.row] == false {
            label.numberOfLines = 0
        } else {
            label.numberOfLines = 1
        }
        
        return cell
    }
    
    // 3. 在 beginUpdates() - endUpdates() 放代碼 有連續(xù)動(dòng)畫效果
    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() //開始
        if label.numberOfLines == 0 {
            label.numberOfLines = 1
            dict[indexPath.row] = true
        } else {
            label.numberOfLines = 0
            dict[indexPath.row] = false
        }
        tableView.endUpdates()
    }

加載失敗
    ##-附加-沒有數(shù)據(jù)時(shí)候提示 可以自行加入空數(shù)據(jù)時(shí)候顯示
    //判斷有沒有數(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)
        }
    }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容