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.lxweimin.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
圖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];
}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 引自:http://m.blog.csdn.net/article/details?id=52180380 記錄一...
    雪_晟閱讀 358評論 0 0
  • 本篇文章記錄了iOS開發零碎知識點,簡單又實用! 代碼寫了這么多,但是總是有些知識點在真正需要用到的時候卻遺忘了,...
    Colin_狂奔的螞蟻閱讀 2,494評論 8 44
  • 記錄一下不常用,但是很實用的知識點,有錯誤請指出,我會更正,有好的知識點也可以提出,我添加上,希望大家共同進步。 ...
    守候的流年閱讀 346評論 0 1
  • 胸藏文墨懷如谷,腹有詩書氣自華! 用十年的時間去做一個行業的頂尖人士,成為一個有趣的靈魂。 你是否能用十年的時間去...
    笛白閱讀 461評論 0 1
  • 時間是一把殺豬刀,轉眼之間,年近四十,兒子天天嚷著,笨死了笨死了!不就奔四了嗎。這個臭小子! 今年過年前,在家里清...
    紀尚辰閱讀 287評論 1 1