效果圖
代碼結(jié)合了day11的學(xué)習(xí)內(nèi)容
1、UI布局
在ViewController加入導(dǎo)航欄
<br />
然后拖入tableView
<br />
拖入一個(gè)tableViewCell
<br />
選中tableViewCell
<br />
創(chuàng)建一個(gè)類(lèi)繼承UITableViewCell
類(lèi)名:FirstTableViewCell
在進(jìn)入storyboard中選擇Cell
<br />
后面的ViewController和這個(gè)類(lèi)似碰声,不做介紹了绑雄。
<br />
2、實(shí)現(xiàn)必要的代理方法
定義屬性:記得把tableView拖入到ViewController中
@IBOutlet weak var tableView: UITableView!
private lazy var data : NSArray = ["測(cè)試數(shù)據(jù)","測(cè)試數(shù)據(jù)","測(cè)試數(shù)據(jù)","測(cè)試數(shù)據(jù)","測(cè)試數(shù)據(jù)","測(cè)試數(shù)據(jù)","測(cè)試數(shù)據(jù)","測(cè)試數(shù)據(jù)"]
<br />
代理方法
// MARK: - Table view data source
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("firstCell", forIndexPath: indexPath) as! FirstTableViewCell
cell.textLabel?.text = data[indexPath.row] as? String
return cell
}
<br />
修改Cell背景顏色
private func changeColor(indexPath:NSIndexPath) -> UIColor{
let green = CGFloat(indexPath.row) / CGFloat(data.count)
return UIColor(red: 1, green: green, blue: 1, alpha: 1)
}
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
cell.backgroundColor = changeColor(indexPath)
}
<br />
設(shè)置代理
tableView.delegate = self
tableView.dataSource = self
automaticallyAdjustsScrollViewInsets = false
//class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource
<br />
實(shí)現(xiàn)動(dòng)畫(huà)
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
animationTable()
}
private func animationTable(){
tableView.reloadData()
//獲取可見(jiàn)的cell
let cells = tableView.visibleCells
//獲取tableview高度
let height : CGFloat = tableView.bounds.height
//遍歷奥邮,并設(shè)置每個(gè)cell的位置
for i in cells {
let cell : UITableViewCell = i as UITableViewCell
cell.transform = CGAffineTransformMakeTranslation(0, height)
}
//設(shè)置動(dòng)畫(huà)
var index = 0.0
//遍歷并設(shè)置動(dòng)畫(huà)
for item in cells {
let cell : UITableViewCell = item as UITableViewCell
UIView.animateWithDuration(1.5, delay: 0.05 * index, usingSpringWithDamping: 0.7, initialSpringVelocity: 0, options: UIViewAnimationOptions.CurveLinear, animations: {
cell.transform = CGAffineTransformMakeTranslation(0, 0)
}, completion: nil)
index += 1
}
}
第二個(gè)頁(yè)面的源碼:附上OC與Swift對(duì)比
//
// SecondTableViewController.swift
// TableViewCellAnimation
//
// Created by ios on 16/9/19.
// Copyright ? 2016年 ios. All rights reserved.
//
import UIKit
class SecondTableViewController: UITableViewController {
private lazy var data : NSArray = ["測(cè)試數(shù)據(jù)","測(cè)試數(shù)據(jù)","測(cè)試數(shù)據(jù)","測(cè)試數(shù)據(jù)","測(cè)試數(shù)據(jù)","測(cè)試數(shù)據(jù)","測(cè)試數(shù)據(jù)","測(cè)試數(shù)據(jù)","測(cè)試數(shù)據(jù)","測(cè)試數(shù)據(jù)","測(cè)試數(shù)據(jù)","測(cè)試數(shù)據(jù)"]
override func viewDidLoad() {
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
//開(kāi)始動(dòng)畫(huà)
animationTable()
}
private func animationTable(){
tableView.reloadData()
//獲取可見(jiàn)的cell
let cells = tableView.visibleCells
//OC
// NSArray * cells = self.talbeView.visibleCells;
//獲取tableview高度
let height : CGFloat = tableView.bounds.height
//OC
// CGFloat height = self.tableView.bounds.size.height;
//遍歷万牺,并設(shè)置每個(gè)cell的位置
for i in cells {
let cell : UITableViewCell = i as UITableViewCell
cell.transform = CGAffineTransformMakeTranslation(0, -height)
}
//OC
// for (UITableViewCell * i in cells) {
// i.transform = CGAffineTransformMakeTranslation(0, -height);
// }
//設(shè)置動(dòng)畫(huà)
var index = 0.0
//OC
// CGFloat index = 0.0;
//遍歷并設(shè)置動(dòng)畫(huà)
for item in cells {
let cell : UITableViewCell = item as UITableViewCell
UIView.animateWithDuration(1.5, delay: 0.05 * index, usingSpringWithDamping: 0.7, initialSpringVelocity: 0, options: UIViewAnimationOptions.CurveLinear, animations: {
cell.transform = CGAffineTransformMakeTranslation(0, 0)
}, completion: nil)
index += 1
}
//OC
// for (UITableViewCell * item in cells) {
// [UIView animateWithDuration:1.5 delay:index * 0.01 usingSpringWithDamping:0.5 initialSpringVelocity:0 options:UIViewAnimationOptionCurveLinear animations:^{
// cell.transform = CGAffineTransformMakeTranslation(0, 0)
// } completion:nil];
// }
}
// MARK: - Table view data source
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return data.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("secondCell", forIndexPath: indexPath) as! SecondTableViewCell
cell.textLabel?.text = data[indexPath.row] as? String
return cell
}
private func changeColor(indexPath:NSIndexPath) -> UIColor{
let green = CGFloat(indexPath.row) / CGFloat(data.count)
return UIColor(red: 1 - (green), green: green, blue: 1, alpha: 1)
}
override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
cell.backgroundColor = changeColor(indexPath)
}
}