美文网首页
记录下开发中遇到的问题

记录下开发中遇到的问题

作者: SinceMeYou | 来源:发表于2020-03-02 17:36 被阅读0次

1.利用UITableView+FDTemplateLayoutCell 和 Masonry 实现cell高度的自动计算

  • 设置UIImageView的约束,高度确定的一定要将高度约束的优先级设置为最高

     [self.icon mas_makeConstraints:^(MASConstraintMaker *make) {    
      make.top.mas_equalTo(10);
       make.left.equalTo(self.name.mas_right).offset(10);
     //此处如果高度确定,一定要将约束的优先级设置为最高,要不会报高度相关约束的警告        
       make.size.mas_equalTo(CGSizeMake(65, 65)).priorityHigh();
    }];
    
  • 需要注意的是不是一行文字的UILabel一定要设置preferredMaxLayoutWidth

2. 对Swizzling中方法的理解

  • 意思就是,在当前的class中添加swizzle方法,方法名为originSelector,如果可以添加成功,说明swizzle方法的方法名在其父类中存在,不成功说明当前swizzle方法在其父类中不存在

     BOOL addMethod =class_addMethod(self, originSelector, method_getImplementation(swizlingMethod), method_getTypeEncoding(swizlingMethod));
    
  • 完整的Swizzling (NSObject的一个分类)

替换实例方法的
+(void)swizzlingInstanceMethodWithOriginSelector:(SEL)originSelector  swizlingSelector:(SEL)swizlingSelector{
    Method originMethod = class_getInstanceMethod (self, originSelector);
     Method swizlingMethod = class_getInstanceMethod(self, swizlingSelector);
    ///此处判断添加的方法是否在原类中存在,不存在的话会添加成功
    ///意思就是,在当前的class中添加swizzle方法,方法名为originSelector,如果可以添加成功,说明swizzle方法的方法名在其父类中存在,不成功说明当前swizzle方法在其父类中不存在
     BOOL addMethod =class_addMethod(self, originSelector, method_getImplementation(swizlingMethod), method_getTypeEncoding(swizlingMethod));
     if(addMethod){ ///可以添加成功,将原有方法的方法名字替换为swizlingSelector
    class_replaceMethod(self, swizlingSelector, method_getImplementation(originMethod), method_getTypeEncoding(originMethod));
    }else{ //不成功的话交互两个方法即可
         method_exchangeImplementations(originMethod, swizlingMethod);
    }
    }
替换类方法的
+(void)swizzlingClassMethodWithOriginSelector:(SEL)originSelector  swizlingSelector:(SEL)swizlingSelector{
Method originMethod = class_getClassMethod(self, originSelector);
Method swizlingMethod = class_getClassMethod(self, swizlingSelector);
Class mateClass = objc_getMetaClass(class_getName([self class]));
BOOL add =class_addMethod(mateClass, originSelector,method_getImplementation(swizlingMethod) , method_getTypeEncoding(swizlingMethod));
if(add){
    class_replaceMethod(mateClass, swizlingSelector, method_getImplementation(originMethod) , method_getTypeEncoding(originMethod));
}else{
    method_exchangeImplementations(originMethod, swizlingMethod);
}
}

相关文章

网友评论

      本文标题:记录下开发中遇到的问题

      本文链接:https://www.haomeiwen.com/subject/bewvkhtx.html