2016-07-21 6 views
0

私は、カスタムUITableViewCellを持つ標準のUITableViewを持っています。私が達成したいのは、次のとおりです。UITableView内部にアニメーションを含む行を挿入する

  • TableViewに表示されている間は、各行は一度に1つずつ消えます。 「スタックにプッシュ」アニメーションをゲットするために - それぞれの新しい行が常にそれによって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() 
    } 
} 

動機:私は基本的に私のdataSourceアレイ内のすべてのデータを排出する必要が1つは、つまりGames.leaderboardに行きます。画面サイズが小さすぎてすべてに収まらない場合は、ユーザーがスクロールダウンする必要はありません。これは私のアプリのUXの非常に重要な部分を形成します。

ご協力いただきありがとうございます。ありがとうございました

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挿入アニメーションは、テーブルに表示される最初の行が一番上にあり、後続の行が底に加えられた。しかし、最新の行、つまり現在のindexPath.rowが常に上に挿入され、古い行がプッシュダウンされるようにしたい - facebook/twitterのフィードアップデートのようなものだが、個々のアニメーションのようなもの –

+0

あなたは 'beginUpdate'を試して、dataSourceにオブジェクトを追加してから、' insertRowAtIndexPath'と 'endUpdate'を呼び出します。 – Tj3n

答えて

0

私は、この値を操作numberOfRowsInSectionNSTimerによって返された値を操作することで、このに近づくでしょう。簡単にするために、私のバージョンでは一定の時間間隔を使用していますが、反復タイマーを使用して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() 
    } 
} 
関連する問題