Myślę, że nie należy przesłonić reloadRowsAtIndexPaths:withRowAnimation
. Wystarczy zaimplementować niestandardową metodę reloadRowsWithMyAnimationAtIndexPaths:
w kategorii UITableView i używać jej, gdy jest potrzebna.
Ale jeśli chcesz, aby zastąpić tę metodę podklasy UITableView jest, można to zrobić:
- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths
withRowAnimation:(UITableViewRowAnimation)animation {
if (self.useMyAnimation)
[self reloadRowsWithMyAnimationAtIndexPaths:indexPaths];
else
[super reloadRowsAtIndexPaths:indexPaths withRowAnimation:animation];
}
self.useMyAnimation tylko flagę (właściwość BOOL), który wskazuje, co animacja używać. Ustaw tę flagę przed operacją ponownego ładowania.
dla 2 lub więcej customAnimations można wdrożyć ENUM:
enum MyTableViewReloadAnimationType {
case None
case First
case Second
case Third
}
następnie dokonać właściwość MyTableViewReloadAnimationType (na przykład, reloadAnimationType) i wybierz odpowiednią metodę animacji z przełącznika:
var reloadAnimationType = MyTableViewReloadAnimationType.None
override func reloadRowsAtIndexPaths(indexPaths: [AnyObject], withRowAnimation animation: UITableViewRowAnimation) {
switch self.reloadAnimationType {
case .None:
super .reloadRowsAtIndexPaths(indexPaths, withRowAnimation:animation)
default:
self .reloadRowsAtIndexPaths(indexPaths, withCustomAnimationType:self.reloadAnimationType)
}
}
func reloadRowsAtIndexPaths(indexPaths: [AnyObject], withCustomAnimationType animationType: MyTableViewReloadAnimationType) {
switch animationType {
case .First:
self .reloadRowsWithFirstAnimationAtIndexPaths(indexPaths)
case .Second:
self .reloadRowsWithSecondAnimationAtIndexPaths(indexPaths)
case .Third:
self .reloadRowsWithThirdAnimationAtIndexPaths(indexPaths)
}
}
Mogłabyś wywołaj metodę niestandardową reloadRowsAtIndexPaths:withCustomAnimationType:
bezpośrednio:
self.tableView .reloadRowsAtIndexPaths([indexPath], withCustomAnimationType: MyTableViewReloadAnimationType.First)
Wewnątrz metody niestandardowej trzeba pobrać aktualną komórkę i nową komórkę z metody DataSource:
func reloadRowsWithFirstAnimationAtIndexPaths(indexPaths: [AnyObject]) {
for indexPath in indexPaths {
var currentCell = self .cellForRowAtIndexPath(indexPath as! NSIndexPath)
var newCell = self.dataSource .tableView(self, cellForRowAtIndexPath: indexPath as! NSIndexPath)
var newCellHeight = self.delegate .tableView(self, heightForRowAtIndexPath: indexPath)
var frame: CGRect = currentCell.frame
frame.size.height = newCellHeight
newCell.frame = frame
self .replaceCellWithFirstAnimation(currentCell!, withAnotherCell: newCell);
}
}
func replaceCellWithFirstAnimation(firstCell : UITableViewCell, withAnotherCell secondCell: UITableViewCell) {
var cellsSuperview = firstCell.superview!
//make this with animation
firstCell .removeFromSuperview()
cellsSuperview .addSubview(secondCell)
}
trzeba obsłużyć sytuację, gdy wysokość newCell za> lub < następnie wysokość currentCell użytkownika. Wszystkie pozostałe ramki komórek muszą zostać ponownie obliczone. Myślę, że można to zrobić za pomocą metod beginUpdates i endUpdates. Zadzwoń do nich przed manipulowaniem komórkami.
Bez obawy o Cel C. Mogę go przeczytać, o ile nie muszę go pisać ... Jeśli chodzi o 'self.useMyAnimation' jak go używasz? Zrobiłem to samo z wyjątkiem tego, że 'if' było o" animacji "nieistniejącej lub nie, ponieważ nie znalazłem innej drogi. Ale to bardziej włamanie niż właściwe rozwiązanie, jeśli mam dwie niestandardowe animacje, to nie działa. A jak przeładować komórkę? – Nico
Zaktualizowałem moją odpowiedź na 2 lub więcej niestandardowych animacji. –
Problem polega na wywołaniu 'reloadRowsAtIndexPaths: withRowAnimation', możesz tylko określić (popraw mnie, jeśli się mylę) typ animacji z' UITableViewRowAnimation'. I jest tylko jedna deklaracja 'reloadRowsAtIndexPaths: withRowAnimation'. Nie mam nic przeciwko temu, aby nie przesłonić tej metody i zachować niestandardową metodę ponownego ładowania, ale jak można przeładować komórkę? – Nico