You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@weex.apache.org by so...@apache.org on 2017/02/28 02:30:27 UTC

[47/50] incubator-weex git commit: * [ios] keep the scroll position when inserting or deleting cells by adjusting the content offset

* [ios] keep the scroll position when inserting or deleting cells by adjusting the content offset


Project: http://git-wip-us.apache.org/repos/asf/incubator-weex/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-weex/commit/27557bae
Tree: http://git-wip-us.apache.org/repos/asf/incubator-weex/tree/27557bae
Diff: http://git-wip-us.apache.org/repos/asf/incubator-weex/diff/27557bae

Branch: refs/heads/0.11-dev
Commit: 27557bae94d67617c4dbb30f3b61aa33004b3863
Parents: fd81196
Author: \u9690\u98ce <cx...@gmail.com>
Authored: Mon Feb 27 14:08:29 2017 +0800
Committer: \u9690\u98ce <cx...@gmail.com>
Committed: Mon Feb 27 14:08:29 2017 +0800

----------------------------------------------------------------------
 .../WeexSDK/Sources/Component/WXCellComponent.h |  1 +
 .../WeexSDK/Sources/Component/WXCellComponent.m |  5 ++
 .../WeexSDK/Sources/Component/WXListComponent.m | 58 +++++++++++++++++---
 3 files changed, 56 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/27557bae/ios/sdk/WeexSDK/Sources/Component/WXCellComponent.h
----------------------------------------------------------------------
diff --git a/ios/sdk/WeexSDK/Sources/Component/WXCellComponent.h b/ios/sdk/WeexSDK/Sources/Component/WXCellComponent.h
index 2e5bf59..2c40cd8 100644
--- a/ios/sdk/WeexSDK/Sources/Component/WXCellComponent.h
+++ b/ios/sdk/WeexSDK/Sources/Component/WXCellComponent.h
@@ -16,5 +16,6 @@
 @property (nonatomic, assign) UITableViewRowAnimation insertAnimation;
 @property (nonatomic, assign) UITableViewRowAnimation deleteAnimation;
 @property (nonatomic, weak) WXListComponent *list;
+@property (nonatomic, assign) BOOL keepScrollPosition;
 
 @end

http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/27557bae/ios/sdk/WeexSDK/Sources/Component/WXCellComponent.m
----------------------------------------------------------------------
diff --git a/ios/sdk/WeexSDK/Sources/Component/WXCellComponent.m b/ios/sdk/WeexSDK/Sources/Component/WXCellComponent.m
index adf91a8..3c35a0b 100644
--- a/ios/sdk/WeexSDK/Sources/Component/WXCellComponent.m
+++ b/ios/sdk/WeexSDK/Sources/Component/WXCellComponent.m
@@ -26,6 +26,7 @@
         _isRecycle = attributes[@"recycle"] ? [WXConvert BOOL:attributes[@"recycle"]] : YES;
         _insertAnimation = [WXConvert UITableViewRowAnimation:attributes[@"insertAnimation"]];
         _deleteAnimation = [WXConvert UITableViewRowAnimation:attributes[@"deleteAnimation"]];
+        _keepScrollPosition = attributes[@"keepScrollPosition"] ? [WXConvert BOOL:attributes[@"keepScrollPosition"]] : NO;
         _lazyCreateView = YES;
         _isNeedJoinLayoutSystem = NO;
     }
@@ -75,6 +76,10 @@
     if (attributes[@"deleteAnimation"]) {
         _deleteAnimation = [WXConvert UITableViewRowAnimation:attributes[@"deleteAnimation"]];
     }
+    
+    if (attributes[@"keepScrollPosition"]) {
+        _keepScrollPosition = [WXConvert BOOL:attributes[@"keepScrollPosition"]];
+    }
 }
 
 - (void)_moveToSupercomponent:(WXComponent *)newSupercomponent atIndex:(NSUInteger)index

http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/27557bae/ios/sdk/WeexSDK/Sources/Component/WXListComponent.m
----------------------------------------------------------------------
diff --git a/ios/sdk/WeexSDK/Sources/Component/WXListComponent.m b/ios/sdk/WeexSDK/Sources/Component/WXListComponent.m
index 4ab6b2c..b5e1b2c 100644
--- a/ios/sdk/WeexSDK/Sources/Component/WXListComponent.m
+++ b/ios/sdk/WeexSDK/Sources/Component/WXListComponent.m
@@ -319,12 +319,10 @@
         WXLogDebug(@"Delete cell:%@ at indexPath:%@", cell.ref, indexPath);
         if (cell.deleteAnimation == UITableViewRowAnimationNone) {
             [UIView performWithoutAnimation:^{
-                [_tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
-                [self handleAppear];
+                [self _deleteTableViewCellAtIndexPath:indexPath keepScrollPosition:cell.keepScrollPosition animation:UITableViewRowAnimationNone];
             }];
         } else {
-            [_tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:cell.deleteAnimation];
-            [self handleAppear];
+            [self _deleteTableViewCellAtIndexPath:indexPath keepScrollPosition:cell.keepScrollPosition animation:cell.deleteAnimation];
         }
     }];
 }
@@ -355,12 +353,10 @@
             _completedSections = completedSections;
             if (cell.insertAnimation == UITableViewRowAnimationNone) {
                 [UIView performWithoutAnimation:^{
-                    [_tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
-                    [self handleAppear];
+                    [self _insertTableViewCellAtIndexPath:indexPath keepScrollPosition:cell.keepScrollPosition animation:UITableViewRowAnimationNone];
                 }];
             } else {
-                [_tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:cell.insertAnimation];
-                [self handleAppear];
+                [self _insertTableViewCellAtIndexPath:indexPath keepScrollPosition:cell.keepScrollPosition animation:cell.insertAnimation];
             }
         } else {
             WXLogInfo(@"Reload cell:%@ at indexPath:%@", cell.ref, indexPath);
@@ -652,6 +648,52 @@
     return [NSIndexPath indexPathForRow:row inSection:section];
 }
 
+- (void)_insertTableViewCellAtIndexPath:(NSIndexPath *)indexPath keepScrollPosition:(BOOL)keepScrollPosition animation:(UITableViewRowAnimation)animation
+{
+    CGFloat adjustment = 0;
+    
+    // keep the scroll position when inserting or deleting cells by adjusting the content offset
+    if (keepScrollPosition) {
+        NSIndexPath *top = _tableView.indexPathsForVisibleRows.firstObject;
+        if ([indexPath compare:top] <= 0) {
+            adjustment = [self tableView:_tableView heightForRowAtIndexPath:indexPath];
+        }
+    }
+    
+    [_tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:animation];
+    
+    if (keepScrollPosition) {
+        CGPoint afterContentOffset = _tableView.contentOffset;
+        CGPoint newContentOffset = CGPointMake(afterContentOffset.x, afterContentOffset.y + adjustment);
+        _tableView.contentOffset = newContentOffset;
+    }
+    
+    [self handleAppear];
+}
+
+- (void)_deleteTableViewCellAtIndexPath:(NSIndexPath *)indexPath keepScrollPosition:(BOOL)keepScrollPosition animation:(UITableViewRowAnimation)animation
+{
+    CGFloat adjustment = 0;
+    
+    // keep the scroll position when inserting or deleting cells by adjusting the content offset
+    if (keepScrollPosition) {
+        NSIndexPath *top = _tableView.indexPathsForVisibleRows.firstObject;
+        if ([indexPath compare:top] <= 0) {
+            adjustment = [self tableView:_tableView heightForRowAtIndexPath:indexPath];
+        }
+    }
+    
+    [_tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:animation];
+    
+    if (keepScrollPosition) {
+        CGPoint afterContentOffset = _tableView.contentOffset;
+        CGPoint newContentOffset = CGPointMake(afterContentOffset.x, afterContentOffset.y - adjustment > 0 ? afterContentOffset.y - adjustment : 0);
+        _tableView.contentOffset = newContentOffset;
+    }
+    
+    [self handleAppear];
+}
+
 - (void)fixFlicker
 {
     static dispatch_once_t onceToken;