2015-02-05 24 views
5

Ho un UITableViewCell che si espande e aggiunge un'etichetta al clic.Animate [tableView reloadData]

Logic:

ho aggiunto un'etichetta cellForRowAtIndexPath alla cella selezionata, e in didSelectRow... I ricaricato tableView. In heightForRow... la cella selezionata si espande. Spero che tu abbia la foto

In didSelectRow... Devo ricaricare Dati e non iniziare/fine aggiornamento. Quello che voglio fare è reloadData e farlo animare. Posso fare così:

[UIView transitionWithView: tableView 
        duration: 0.40f 
        options: UIViewAnimationOptionTransitionCrossDissolve 
       animations: ^(void) 
{ 
    [self.tableView reloadData]; 
} 
       completion: nil 
}]; 

In tal modo una croce si dissolve delle celle che sta cambiando. Quello che voglio è UITableViewRowAnimationTop, ma a quanto pare non è possibile nelle opzioni di transizione. C'è un modo per reloadData e utilizzare UITableViewRowAnimationTop?

+0

Chiamare 'reloadData' distrugge tutte le celle attualmente nella vista tabella e ricostruisce tutto da zero. Forse stai cercando un metodo diverso se questo non è il comportamento che desideri. –

+0

Quindi non c'è modo di usare 'UITableViewRowAnimationTop' con reloadData? –

+0

Hai provato a utilizzare [beginUpdates] (https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableView_Class/index.html # // apple_ref/occ/instm/UITableView/beginUpdates) e un blocco di aggiornamento? – matt

risposta

8

[UITablewView reloadData:] non è animabile. Tuttavia, potresti provare a ricaricare tableView ricaricando tutte le sezioni con l'animazione.

NSIndexSet *sections = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, [tableView numberOfSections])]; 

[self.tableView reloadSections:sections withRowAnimation:UITableViewRowAnimationAutomatic]; 

OK, ho mentito. Può essere animabile ma richiede molta conoscenza su come QuartzCore Framework funziona su componenti dell'interfaccia utente.

@import QuartzCore; 

[self.tableView reloadData]; 

CATransition *transition = [CATransition animation]; 
transition.type = kCATransitionPush; 
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; 
transition.fillMode = kCAFillModeForwards; 
transition.duration = 0.6; 
transition.subtype = kCATransitionFromTop; 

[self.tableView.layer addAnimation:transition forKey:@"UITableViewReloadDataAnimationKey"]; 
+0

Non c'è 'animation' a xcodes intelligent inspector. –

+0

Non ho capito cosa volevi dire. 'CATransition' ha un metodo di classe chiamato' transition'. Sei sicuro di non scrivere 'CATransaction'? – ozgur

+0

Hai ragione, stavo usando 'CATransaction'. L'ho appena provato e quello che succede è che l'intera tableView scende e poi torna indietro e il tavolo non si ricarica nemmeno? –

7

Una soluzione molto più semplice che non richiede Core Animation.

Swift 3:

UIView.transition(with: self.view, 
        duration: 0.15, 
        options: [.curveEaseInOut, .transitionCrossDissolve], 
        animations: { 
         self.tableView.reloadRows(at: self.tableView.indexPathsForVisibleRows!, with: .none) 
}, completion: nil) 
1

O questo:

NSIndexSet *sections = [NSIndexSet indexSetWithIndexesInRange: NSMakeRange(0, [self numberOfSectionsInTableView: self.tableView])]; 
    [self.tableView reloadSections: sections withRowAnimation: UITableViewRowAnimationTop]; 

Personalmente, penso che UITableViewRowAnimationFade sembra molto meglio tho.

0

Questa estensione UITableView funziona per Swift 4:

extension UITableView { func reloadDataWithAnimation(duration: TimeInterval, options: UIViewAnimationOptions) { UIView.animate(withDuration: duration, delay: 0.0, options: options, animations: { self.alpha = 0 }, completion: nil) self.reloadData() UIView.animate(withDuration: duration, delay: 0.0, options: options, animations: { self.alpha = 1 }, completion: nil) }

Usage: tableView.reloadDataWithAnimation(duration: 1.0, options: .curveLinear)

+0

Questo approccio utilizza 'reloadData()' in modo che consenta l'animazione degli aggiornamenti alla struttura di tableView (non solo inserimenti di righe o sezioni, eliminazioni o aggiornamenti, ma anche il refetching o la modifica dell'origine dati). – fivewood

0

Swift soluzione 4 con QuartzCore

import QuartzCore 

tableView.reloadData() 

let transition = CATransition() 
transition.type = kCATransitionPush 
transition.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut) 
transition.fillMode = kCAFillModeForwards 
transition.duration = 0.6 
transition.subtype = kCATransitionFromBottom 

tableView.layer.add(transition, forKey: "UITableViewReloadDataAnimationKey")