iOS开发零碎知识点

UITableView分割线置顶

/**
*  分割线顶头
*/
-(void)viewDidLayoutSubviews
{
 if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
     [self.tableView setSeparatorInset:UIEdgeInsetsMake(0,0,0,0)];
 }

 if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
     [self.tableView setLayoutMargins:UIEdgeInsetsMake(0,0,0,0)];
 }
}
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
 if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
     [cell setSeparatorInset:UIEdgeInsetsZero];
 }
 if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
     [cell setLayoutMargins:UIEdgeInsetsZero];
 }
}

作者:StrongX
链接:http://www.jianshu.com/p/378ca60232ef
來源:
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

1.NSArray 快速求总&&最大值&&最小值&&平均值

- (void)getAveageValue {
    NSMutableArray *array = [[NSMutableArray alloc]init];

    ModelItem *item1 = [[ModelItem alloc]init];
    item1.percent = 30;
    item1.name    = @"item1";
    [array addObject:item1];

    ModelItem *item2 = [[ModelItem alloc]init];
    item2.percent = 20;
    item2.name    = @"item2";
    [array addObject:item2];

    ModelItem *item3 = [[ModelItem alloc]init];
    item3.percent = 10;
    item3.name    = @"item3";
    [array addObject:item3];
    
    CGFloat sum = [[array valueForKeyPath:@"@sum.percent.floatValue"] floatValue];
    CGFloat avg = [[array valueForKeyPath:@"@avg.percent.floatValue"] floatValue];
    CGFloat max = [[array valueForKeyPath:@"@max.percent.floatValue"] floatValue];
    CGFloat min = [[array valueForKeyPath:@"@min.percent.floatValue"] floatValue];
    NSLog(@"\n总和:%f\n平均:%f\n最大:%f\n最小:%f",sum,avg,max,min);
}

//模型对象
@interface ModelItem : NSObject
@property (nonatomic,assign) CGFloat percent;
@property (nonatomic,  copy) NSString *name;
@end
iOS开发零碎知识点_第1张图片
图1.png

获取沙盒根目录

NSString *directory = NSHomeDirectory();
NSLog(@"directory:%@", directory);

2.UITableView,取消区头停滞效果

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat sectionHeaderHeight = sectionHead.height;
    if (scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView;.contentOffset.y>=0)
    {
        scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
    }
    else if(scrollView.contentOffset.y>=sectionHeaderHeight)
    {
        scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
    }
}

3.获取某个view所在的控制器

- (UIViewController *)viewController
{
  UIViewController *viewController = nil;  
  UIResponder *next = self.nextResponder;
  while (next)
  {
    if ([next isKindOfClass:[UIViewController class]])
    {
      viewController = (UIViewController *)next;      
      break;    
    }    
    next = next.nextResponder;  
  } 
    return viewController;
}

4.两种方法删除NSUserDefaults所有记录

//方法一
NSString *appDomain = [[NSBundle mainBundle] bundleIdentifier];
[[NSUserDefaults standardUserDefaults] removePersistentDomainForName:appDomain];


//方法二
- (void)resetDefaults
{
    NSUserDefaults * defs = [NSUserDefaults standardUserDefaults];
    NSDictionary * dict = [defs dictionaryRepresentation];
    for (id key in dict)
    {
        [defs removeObjectForKey:key];
    }
    [defs synchronize];
}

5.UIImage 占用内存大小

UIImage *image = [UIImage imageNamed:@"aa"];
NSUInteger size  = CGImageGetHeight(image.CGImage) * CGImageGetBytesPerRow(image.CGImage);

6.去掉导航栏返回的back标题

[[UIBarButtonItemappearance]setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60)forBarMetrics:UIBarMetricsDefault];

7.JSPatch 的frame对象的使用

require('UIColor,UIView');//第一个坑,需要导入对应的控制器名称
defineClass('ViewController', {
    initSubView: function() {

       //第二个坑,没有CGRectMake(100,100,100,100),用initWithFrame({x:100,y:100,width:100,heigt:100});

        var view = UIView.alloc().initWithFrame({x:100, y:100, width:100, height:100});
        view.setBackgroundColor(UIColor.blackColor());
        self.view().addSubview(view);
    },
});


//将图片存到本地
- (void)AViewDidLoad {
 [[SDWebImageManager sharedManager] downloadImageWithURL:[NSURL URLWithString:launchImageEntity.result[0].imageUrl] options:SDWebImageRetryFailed progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
        
        if (image) {
            
            [weakSelf downloadLaunchImageSuccessWithImage:image
                                             andImageName:imageName];
            
            [CommonMethod setUserdefaultWithValue:[NSNumber numberWithDouble:curUpdateTime]
                                           forKey:updateTime];
        }
        
    }];
}

-(void)downloadLaunchImageSuccessWithImage:(UIImage *)image
                              andImageName:(NSString *)imageName
{
    
    NSString *documentPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
    
    //设置图片路径
    NSString *imagePath = [documentPath stringByAppendingString:[NSString stringWithFormat:@"/%@.png",imageName]];
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    
    if ([fileManager fileExistsAtPath:imagePath]) {
        
        [fileManager removeItemAtPath:imagePath
                                error:nil];
    }
    
    if (image) {
        
        [UIImagePNGRepresentation(image) writeToFile:imagePath
                                          atomically:YES];
    }
    
    
}

//取出本地保存的图片
- (void)BViewDidLoad {
    //取本地保存的首页广告图
    NSString *documentPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
    NSString *imagePath = [documentPath stringByAppendingString:[NSString stringWithFormat:@"/%@.png",EntrustTopImageName]];
    UIImage *image = [UIImage imageWithContentsOfFile:imagePath];
}

你可能感兴趣的:(iOS开发零碎知识点)