Skip to content

Commit

Permalink
* [ios] Stupid scoll view, always reset content offset to zero after …
Browse files Browse the repository at this point in the history
…insert cells, fixed it by hooking
  • Loading branch information
cxfeng1 committed Jun 22, 2016
1 parent 4c9dad7 commit 981dcd6
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 0 deletions.
8 changes: 8 additions & 0 deletions ios/sdk/WeexSDK.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@
7469869C1C4DEAC20054A57E /* libicucore.tbd in Frameworks */ = {isa = PBXBuildFile; fileRef = 7469869B1C4DEAC20054A57E /* libicucore.tbd */; };
7469869F1C4E2C000054A57E /* NSArray+Weex.h in Headers */ = {isa = PBXBuildFile; fileRef = 7469869D1C4E2C000054A57E /* NSArray+Weex.h */; };
746986A01C4E2C010054A57E /* NSArray+Weex.m in Sources */ = {isa = PBXBuildFile; fileRef = 7469869E1C4E2C000054A57E /* NSArray+Weex.m */; };
74896F301D1AC79400D1D593 /* NSObject+WXSwizzle.h in Headers */ = {isa = PBXBuildFile; fileRef = 74896F2E1D1AC79400D1D593 /* NSObject+WXSwizzle.h */; };
74896F311D1AC79400D1D593 /* NSObject+WXSwizzle.m in Sources */ = {isa = PBXBuildFile; fileRef = 74896F2F1D1AC79400D1D593 /* NSObject+WXSwizzle.m */; };
748B25181C44A6F9005D491E /* WXSDKInstance_private.h in Headers */ = {isa = PBXBuildFile; fileRef = 748B25161C44A6F9005D491E /* WXSDKInstance_private.h */; };
74915F471C8EB02B00BEBCC0 /* WXAssert.h in Headers */ = {isa = PBXBuildFile; fileRef = 74915F451C8EB02B00BEBCC0 /* WXAssert.h */; };
74915F481C8EB02B00BEBCC0 /* WXAssert.m in Sources */ = {isa = PBXBuildFile; fileRef = 74915F461C8EB02B00BEBCC0 /* WXAssert.m */; };
Expand Down Expand Up @@ -286,6 +288,8 @@
7469869B1C4DEAC20054A57E /* libicucore.tbd */ = {isa = PBXFileReference; lastKnownFileType = "sourcecode.text-based-dylib-definition"; name = libicucore.tbd; path = usr/lib/libicucore.tbd; sourceTree = SDKROOT; };
7469869D1C4E2C000054A57E /* NSArray+Weex.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSArray+Weex.h"; sourceTree = "<group>"; };
7469869E1C4E2C000054A57E /* NSArray+Weex.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSArray+Weex.m"; sourceTree = "<group>"; };
74896F2E1D1AC79400D1D593 /* NSObject+WXSwizzle.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSObject+WXSwizzle.h"; sourceTree = "<group>"; };
74896F2F1D1AC79400D1D593 /* NSObject+WXSwizzle.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSObject+WXSwizzle.m"; sourceTree = "<group>"; };
748B25161C44A6F9005D491E /* WXSDKInstance_private.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WXSDKInstance_private.h; sourceTree = "<group>"; };
74915F451C8EB02B00BEBCC0 /* WXAssert.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WXAssert.h; sourceTree = "<group>"; };
74915F461C8EB02B00BEBCC0 /* WXAssert.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = WXAssert.m; sourceTree = "<group>"; };
Expand Down Expand Up @@ -690,6 +694,8 @@
74A4BA991CB3BAA100195969 /* WXThreadSafeMutableDictionary.m */,
7461F8A61CFC33A800F62D44 /* WXThreadSafeMutableArray.h */,
7461F8A71CFC33A800F62D44 /* WXThreadSafeMutableArray.m */,
74896F2E1D1AC79400D1D593 /* NSObject+WXSwizzle.h */,
74896F2F1D1AC79400D1D593 /* NSObject+WXSwizzle.m */,
);
path = Utility;
sourceTree = "<group>";
Expand Down Expand Up @@ -854,6 +860,7 @@
59A596311CB632050012CD52 /* WXRootViewController.h in Headers */,
77E65A151C155EB5008B8775 /* WXTextComponent.h in Headers */,
74CC7A1C1C2BC5F800829368 /* WXCellComponent.h in Headers */,
74896F301D1AC79400D1D593 /* NSObject+WXSwizzle.h in Headers */,
59A596241CB6311F0012CD52 /* WXStorageModule.h in Headers */,
74A4BA851CAD453400195969 /* WXNetworkProtocol.h in Headers */,
7461F8A81CFC33A800F62D44 /* WXThreadSafeMutableArray.h in Headers */,
Expand Down Expand Up @@ -1038,6 +1045,7 @@
77E65A071C10507B008B8775 /* WXModuleManager.m in Sources */,
77D161291C02DE1A0010B15B /* WXSDKManager.m in Sources */,
7461F8911CFB373100F62D44 /* WXDisplayQueue.m in Sources */,
74896F311D1AC79400D1D593 /* NSObject+WXSwizzle.m in Sources */,
746986A01C4E2C010054A57E /* NSArray+Weex.m in Sources */,
77E65A0E1C155E99008B8775 /* WXDivComponent.m in Sources */,
2A60CE9D1C91733E00857B9F /* WXSwitchComponent.m in Sources */,
Expand Down
21 changes: 21 additions & 0 deletions ios/sdk/WeexSDK/Sources/Component/WXListComponent.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#import "NSArray+Weex.h"
#import "WXAssert.h"
#import "WXUtility.h"
#import "NSObject+WXSwizzle.h"
#import "WXSDKInstance_private.h"

@interface WXTableView : UITableView
Expand Down Expand Up @@ -55,6 +56,7 @@ - (instancetype)initWithRef:(NSString *)ref type:(NSString *)type styles:(NSDict
_completedCells = [NSMutableArray wx_mutableArrayUsingWeakReferences];

[self fillPadding];
[self fixFlicker];
}

return self;
Expand All @@ -71,6 +73,7 @@ - (void)fillPadding
}
}


- (UIView *)loadView
{
return [[WXTableView alloc] init];
Expand Down Expand Up @@ -329,5 +332,23 @@ - (BOOL)isNeedLoadMore
return superNeedLoadMore && _previousLoadMoreRowNumber != [self tableView:_tableView numberOfRowsInSection:0];
}

- (void)fixFlicker
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
//(ง •̀_•́)ง┻━┻ Stupid scoll view, always reset content offset to zero after insert cells, any other more elegant way?
NSString *a = @"ntOffsetIfNe";
NSString *b = @"adjustConte";

NSString *originSelector = [NSString stringWithFormat:@"_%@%@cessary", b, a];
[[self class] weex_swizzle:[WXTableView class] Method:NSSelectorFromString(originSelector) withMethod:@selector(fixedFlickerSelector)];
});
}

- (void)fixedFlickerSelector
{
// DO NOT delete this method.
}


@end
16 changes: 16 additions & 0 deletions ios/sdk/WeexSDK/Sources/Utility/NSObject+WXSwizzle.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* Created by Weex.
* Copyright (c) 2016, Alibaba, Inc. All rights reserved.
*
* This source code is licensed under the Apache Licence 2.0.
* For the full copyright and license information,please view the LICENSE file in the root directory of this source tree.
*/


#import <Foundation/Foundation.h>

@interface NSObject (WXSwizzle)

+ (BOOL)weex_swizzle:(Class)originalClass Method:(SEL)originalSelector withMethod:(SEL)swizzledSelector;

@end
36 changes: 36 additions & 0 deletions ios/sdk/WeexSDK/Sources/Utility/NSObject+WXSwizzle.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* Created by Weex.
* Copyright (c) 2016, Alibaba, Inc. All rights reserved.
*
* This source code is licensed under the Apache Licence 2.0.
* For the full copyright and license information,please view the LICENSE file in the root directory of this source tree.
*/

#import "NSObject+WXSwizzle.h"
#import <objc/runtime.h>

@implementation NSObject (WXSwizzle)

+ (BOOL)weex_swizzle:(Class)originalClass Method:(SEL)originalSelector withMethod:(SEL)swizzledSelector
{
if (!(originalClass && originalSelector && swizzledSelector)) {
return NO;
}

Class class = [self class];

Method originalMethod = class_getInstanceMethod(originalClass, originalSelector);
Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);

BOOL didAddMethod = class_addMethod(originalClass, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod));

if (didAddMethod) {
class_replaceMethod(class, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));
} else {
method_exchangeImplementations(originalMethod, swizzledMethod);
}

return YES;
}

@end

0 comments on commit 981dcd6

Please sign in to comment.