2016-07-21 26 views
0

動畫頂端插入行我有標準的UITableView與自定義的UITableViewCell。我想要實現的是:在裏面的UITableView

  • 每行都應該在顯示在TableView上時一次淡入。

  • 但是,應注意每個新行應始終插入頂部,從而將先前存在的行向下推動1個單元 - 以獲得「推入堆棧」動畫。

這裏是我到現在爲止,這確實與香草褪色個體插入的,但在底部:

func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) { 
    cell.alpha = 0 
    let delayBetweenRowInserts = 0.5 + Double(indexPath.row) * 1.0; //calculate delay 
    UIView.animateWithDuration(1, delay: delayBetweenRowInserts, options: .TransitionCurlUp, animations: { 
    cell.alpha = 1.0 
    }, completion: nil 
) 
} 

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    if let cell = leaderboardTableView.dequeueReusableCellWithIdentifier(CUSTOM_CELL) as? CustomTableViewCell { 
     let playerName = Array(Games.leaderboard[indexPath.row].keys)[0] 
     let playerScore = Array(Games.leaderboard[indexPath.row].values)[0] 
     cell.configureCell(playerName, score: "\(playerScore)") 
     return cell 
    } else { 
     return UITableViewCell() 
    } 
} 

動機:我基本上都用盡所有的數據在我的數據源陣列一個去,即Games.leaderboard而不依賴於用戶的情況下,向下滾動他的屏幕尺寸太小,放不下的一切。這構成了我的應用程序用戶體驗中非常關鍵的一部分。

任何幫助,非常感謝!謝謝

P.S作爲替代方法,我嘗試使用beginUpdates().Top: 我試過使用beginUpdates()。在我看來,它不會改變動畫行的索引。我打電話startInsertingRows()數據源後,已準備好

func startInsertingRows() { 
    var indexPaths = [NSIndexPath]() 
    for index in 0...Games.leaderboard.count-1 { 
     indexPaths.append(NSIndexPath(forItem: Games.leaderboard.count-1-index, inSection: 0)) 
    } 
    leaderboardTableView.beginUpdates() 
    leaderboardTableView.insertRowsAtIndexPaths((indexPaths), withRowAnimation: .Top) 
    leaderboardTableView.endUpdates() 
} 
+0

並不真正瞭解你的REQ,但如果你想推的動畫,你可以用'讓部分=的NSIndexSet(indexesInRange:NSMakeRange(0,self.tableView.numberOfSections))''然後self.tableView.reloadSections (節,withRowAnimation:.Automatic)'而不是'tableView.reloadData' – Tj3n

+0

@ Tj3n根據我當前的代碼: -row插入動畫發生在第一行出現在表上並且後續行得到添加到底部。 -However,我想是最新的行,要始終在頂部插入即當前indexPath.row及以上行被推下來 - 有點像一個Facebook/Twitter的飼料更新,但有個別動畫 –

+0

它還挺容易的不是它,有你試過'beginUpdate',添加對象到您的數據源,然後調用'insertRowAtIndexPath'和'endUpdate' – Tj3n

回答

0

我會通過操縱和NSTimer操縱這個值由numberOfRowsInSection返回的值接近這一點。爲簡單起見,我的版本使用一個固定的時間間隔,但你可以在tableTick再次使用非重複計時器和調度。另外,我開球在viewDidAppear動畫,但如果數據尚未加載,可能不適合你的情況,但可以根據需要調整。

var tableTimer: NSTimer? 
var cellPointer = 0 

override func viewDidAppear(animated: Bool) { 
    super.viewDidAppear(animated) 
    cellPointer = Games.leaderboard.count 
    self.tableTimer = NSTimer.scheduledTimerWithTimeInterval(0.5, target: self, selector: #selector(ViewController.tableTick), userInfo: nil, repeats: true) 
} 

func tableTick() { 
    if cellPointer > 0 { 
     cellPointer -= 1 
     self.tableView.insertRowsAtIndexPaths([NSIndexPath(forRow: 0, inSection: 0)], withRowAnimation: .Fade) 
    } else { 
     self.tableTimer!.invalidate() 
     self.tableTimer = nil 
    } 
} 

override func viewWillDisappear(animated: Bool) { 
    super.viewWillDisappear(animated) 
    if let timer = self.tableTimer { 
     timer.invalidate() 
     self.tableTimer = nil 
    } 
} 

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return Games.leaderboard.count-self.cellPointer 
} 

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    if let cell = leaderboardTableView.dequeueReusableCellWithIdentifier(CUSTOM_CELL) as? CustomTableViewCell { 
     let index = cellPointer + indexPath.row 
     let playerName = Array(Games.leaderboard[index].keys)[0] 
     let playerScore = Array(Games.leaderboard[index].values)[0] 
     cell.configureCell(playerName, score: "\(playerScore)") 
     return cell 
    } else { 
     return UITableViewCell() 
    } 
}