Merge pull request #722 from nguyenhuy/RelayoutCellAPI

Add relayout item/row APIs to ASTableView and ASCollectionView
This commit is contained in:
appleguy
2015-10-25 21:28:20 -07:00
8 changed files with 57 additions and 8 deletions

View File

@@ -123,7 +123,6 @@ static const CGFloat kFontSize = 18.0f;
_textNode.attributedString = [[NSAttributedString alloc] initWithString:_text _textNode.attributedString = [[NSAttributedString alloc] initWithString:_text
attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:kFontSize]}]; attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:kFontSize]}];
[self invalidateCalculatedLayout];
} }
@end @end

View File

@@ -223,6 +223,16 @@
*/ */
- (void)reloadItemsAtIndexPaths:(NSArray *)indexPaths; - (void)reloadItemsAtIndexPaths:(NSArray *)indexPaths;
/**
* Relayouts the specified item.
*
* @param indexPath The index path identifying the item to relayout.
*
* @discussion This method must be called from the main thread. The relayout is excuted on main thread.
* The node of the specified item must be updated to cause layout changes before this method is called.
*/
- (void)relayoutItemAtIndexPath:(NSIndexPath *)indexPath;
/** /**
* Moves the item at a specified location to a destination location. * Moves the item at a specified location to a destination location.
* *

View File

@@ -458,6 +458,14 @@ static BOOL _isInterceptedSelector(SEL sel)
[_dataController reloadRowsAtIndexPaths:indexPaths withAnimationOptions:kASCollectionViewAnimationNone]; [_dataController reloadRowsAtIndexPaths:indexPaths withAnimationOptions:kASCollectionViewAnimationNone];
} }
- (void)relayoutItemAtIndexPath:(NSIndexPath *)indexPath
{
ASDisplayNodeAssertMainThread();
ASCellNode *node = [self nodeForItemAtIndexPath:indexPath];
[node setNeedsLayout];
[super reloadItemsAtIndexPaths:@[indexPath]];
}
- (void)moveItemAtIndexPath:(NSIndexPath *)indexPath toIndexPath:(NSIndexPath *)newIndexPath - (void)moveItemAtIndexPath:(NSIndexPath *)indexPath toIndexPath:(NSIndexPath *)newIndexPath
{ {
ASDisplayNodeAssertMainThread(); ASDisplayNodeAssertMainThread();

View File

@@ -556,9 +556,13 @@ typedef void (^ASDisplayNodeDidLoadBlock)(ASDisplayNode *node);
* If this node was measured, calling this method triggers an internal relayout: the calculated layout is invalidated, * If this node was measured, calling this method triggers an internal relayout: the calculated layout is invalidated,
* and the supernode is notified or (if this node is the root one) a full measurement pass is executed using the old constrained size. * and the supernode is notified or (if this node is the root one) a full measurement pass is executed using the old constrained size.
* *
* Note: If the relayout causes a change in size of the root node that is attached to a container view * Note: If the relayout causes a change in size of the root node that is attached to a container view,
* (table or collection view, for example), the container view must be notified to relayout. * the container view must be notified to relayout.
* For ASTableView and ASCollectionView, an empty batch editing transaction must be triggered to animate to new row / item sizes. * For ASTableView and ASCollectionView, instead of calling this method directly,
* it is recommended to call -relayoutRowAtIndexPath:withRowAnimation and -relayoutItemAtIndexPath: respectively.
*
* @see [ASTableView relayoutRowAtIndexPath:withRowAnimation:]
* @see [ASCollectionView relayoutItemAtIndexPath:]
*/ */
- (void)setNeedsLayout; - (void)setNeedsLayout;

View File

@@ -214,6 +214,18 @@
*/ */
- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation; - (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;
/**
* Relayouts the specified row using a given animation effect.
*
* @param indexPath The index path identifying the row to relayout.
*
* @param animation A constant that indicates how the relayout is to be animated. See UITableViewRowAnimation.
*
* @discussion This method must be called from the main thread. The relayout is excuted on main thread.
* The node of the specified row must be updated to cause layout changes before this method is called.
*/
- (void)relayoutRowAtIndexPath:(NSIndexPath *)indexPath withRowAnimation:(UITableViewRowAnimation)animation;
/** /**
* Moves the row at a specified location to a destination location. * Moves the row at a specified location to a destination location.
* *

View File

@@ -466,6 +466,14 @@ void ASPerformBlockWithoutAnimation(BOOL withoutAnimation, void (^block)()) {
[_dataController reloadRowsAtIndexPaths:indexPaths withAnimationOptions:animation]; [_dataController reloadRowsAtIndexPaths:indexPaths withAnimationOptions:animation];
} }
- (void)relayoutRowAtIndexPath:(NSIndexPath *)indexPath withRowAnimation:(UITableViewRowAnimation)animation
{
ASDisplayNodeAssertMainThread();
ASCellNode *node = [self nodeForRowAtIndexPath:indexPath];
[node setNeedsLayout];
[super reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:animation];
}
- (void)moveRowAtIndexPath:(NSIndexPath *)indexPath toIndexPath:(NSIndexPath *)newIndexPath - (void)moveRowAtIndexPath:(NSIndexPath *)indexPath toIndexPath:(NSIndexPath *)newIndexPath
{ {
ASDisplayNodeAssertMainThread(); ASDisplayNodeAssertMainThread();

View File

@@ -185,13 +185,22 @@ static const CGFloat kInnerPadding = 10.0f;
- (void)toggleImageEnlargement - (void)toggleImageEnlargement
{ {
_isImageEnlarged = !_isImageEnlarged; _isImageEnlarged = !_isImageEnlarged;
[self setNeedsLayout];
} }
- (void)toggleNodesSwap - (void)toggleNodesSwap
{ {
_swappedTextAndImage = !_swappedTextAndImage; _swappedTextAndImage = !_swappedTextAndImage;
[self setNeedsLayout];
[UIView animateWithDuration:0.15 animations:^{
self.alpha = 0;
} completion:^(BOOL finished) {
[self setNeedsLayout];
[self.view layoutIfNeeded];
[UIView animateWithDuration:0.15 animations:^{
self.alpha = 1;
}];
}];
} }
@end @end

View File

@@ -118,11 +118,10 @@ static const NSInteger kMaxLitterSize = 100; // max number of kitten cell
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{ {
[_tableView deselectRowAtIndexPath:indexPath animated:YES]; [_tableView deselectRowAtIndexPath:indexPath animated:YES];
[_tableView beginUpdates];
// Assume only kitten nodes are selectable (see -tableView:shouldHighlightRowAtIndexPath:). // Assume only kitten nodes are selectable (see -tableView:shouldHighlightRowAtIndexPath:).
KittenNode *node = (KittenNode *)[_tableView nodeForRowAtIndexPath:indexPath]; KittenNode *node = (KittenNode *)[_tableView nodeForRowAtIndexPath:indexPath];
[node toggleImageEnlargement]; [node toggleImageEnlargement];
[_tableView endUpdates]; [_tableView relayoutRowAtIndexPath:indexPath withRowAnimation:UITableViewRowAnimationAutomatic];
} }
- (ASCellNode *)tableView:(ASTableView *)tableView nodeForRowAtIndexPath:(NSIndexPath *)indexPath - (ASCellNode *)tableView:(ASTableView *)tableView nodeForRowAtIndexPath:(NSIndexPath *)indexPath