iOS -小知识集

知识小集目录

  • 背景透明,子控件不透明
  • iOS11之后 约束刘海
  • UICollectionView 跳转到指定section 常见用于商品详情
  • 字符串截取
  • 导入json、geojson文件注意事项
  • 真机模拟器区分
  • isExclusiveTouch的使用
  • UIButton 加载网路图片
  • tableview 上拉加载更多不停地请求
  • tableview cell 上button 无响应
  • 系统版本判断

  • iOS 除法
  • 数组添加另一个数组或者数组元素
  • 表格控件刷新单个section或者row
  • 数组、字符串互转
  • 数组的截取
  • Masonry 自适应文字宽度、高度
  • 更改xcode支持的当前ios版本
  • tableview 点击cell动画效果

_bottomView.backgroundColor = [[UIColor akext_colorWithHex:@"#0437F1"] colorWithAlphaComponent:0.5];
 _bgView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.6];
  • iOS11之后 约束刘海
if (@available(iOS 11.0, *)) {
    _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
} else {
    // Fallback on earlier versions
    self.automaticallyAdjustsScrollViewInsets = NO;
}

  • UICollectionView 跳转到指定section 常见用于商品详情
[self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:indexPath.item inSection:0] atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
  • 字符串截取
substringFromIndex:从Index开始截取到最后

substringToIndex:从最前头一直截取到Index
  • 导入json、geojson文件注意事项
    image.png

    如果这两不添加是找不到该本地文件的
  NSString *fileName = [[NSBundle mainBundle] pathForResource:@"StoreInfoDat" ofType:@"geojson"];
    //check file exists
    if (fileName) {
      //retrieve file content
      NSData *partyData = [[NSData alloc] initWithContentsOfFile:fileName];

      //convert JSON NSData to a usable NSDictionary
      NSError *error;
      NSDictionary *party = [NSJSONSerialization JSONObjectWithData:partyData
        options:0
          error:&error];
      if (error) {
        NSLog(@"Something went wrong! %@", error.localizedDescription);
      }
      else {
        NSLog(@"party info: %@", party);
      }
    }
   else {
      NSLog(@"Couldn't find file!\n\n");
    }
  • 真机模拟器区分
 #if TARGET_IPHONE_SIMULATOR//模拟器
    #elif TARGET_OS_IPHONE//真机
    #endif
  • isExclusiveTouch的使用

待续...

  • UIButton 加载网路图片

导入SDWebImage库文件
在应用的文件中添加#import 头文件

[btn.imageView sd_setImageWithURL:[NSURL URLWithString:@"按钮url地址"] placeholderImage:[UIImage imageNamed:@"占位图片"]];
  • tableview 上拉加载更多不停地请求
   /** 放置tableview 不停的刷新 */
        _baseTableView.estimatedRowHeight = 0;
        _baseTableView.estimatedSectionHeaderHeight = 0;
        _baseTableView.estimatedSectionFooterHeight = 0;
  • tableview上 cell button 点击无响应

最关键的是:按钮要添加到cell的contentview上。否则点击不会生效。

  • 系统版本判断
if (@available(iOS 11.0, *)) {
    NSLog(@"iOS 11以上版本");
} else {
    NSLog(@"iOS 11以下版本");
}
  • iOS除法
下面两个代码的运行,你就会发现区别了。

//结果为0.468
CGFloat tFloat = (CGFloat)375/800;
//结果为0
CGFloat tFloat = 375/800;
//结果为0
CGFloat tFloat = 1/0;
//结果为+lnf 代表正无穷
CGFloat tFloat = (CGFloat)1/0;
原因:两个整数相除,结果也为整数
  • 数组添加另一个数组或者数组元素
//        //插入的位置
//        NSMutableIndexSet *indexSet = [NSMutableIndexSet indexSetWithIndexesInRange:NSMakeRange(1, array.count)];
//        [self.dataArray insertObjects:array atIndexes:indexSet];
[self.dataArray insertObject:array atIndex:1];

一定要可变数组才行

//可变-不可变
NSArray *myArray = [NSArray arrayWithArray: myMutableArray];
//不可变-可变
self.dataArray = [NSMutableArray arrayWithArray:tempArray];
  • 表格控件刷新单个section或者row
[self.baseCollectionView reloadItemsAtIndexPaths:[NSArray arrayWithObjects:[NSIndexPath indexPathForRow:0 inSection:2], nil]];
  • 数组字符串互转

1.将array数组转换为string字符串

 NSString *str = [array componentsJoinedByString:@"#"];//#为分隔符

输入:[str,kkk,jjj]
输出:str#kkk#jjj

2.将string字符串拆分为array数组[有分割符]

NSArray *array = [str componentsSeparatedByString:@"#"];//#为分隔符

输入: str#kkk#jjj
输出: [str,kkk,jjj]

  • 数组的截取
NSArray *array = [NSArray arrayWithObjects:@"Crystal",@"Maisie",@"Lukas",@"Ruben",@"Brooklynn",@"Melody",@"Imogen",@"Skye",@"Rafael",@"Lindsey",@"Felix",nil];
NSArray *topThree = [array subarrayWithRange:NSMakeRange(0, 3)];
NSArray *remaining = [array subarrayWithRange:NSMakeRange(3, array.count-3)];
NSLog(@"topThree:%@=======remaining:%@",topThree,remaining);

  • Masonry 自适应 文字 宽度、高度
[self.storeNameLabel setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
  • 更改xcode支持的当前ios版本


    image.png
image.png
  • tableview 点击cell动画效果 deselectRowAtIndexPath:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

你可能感兴趣的:(iOS -小知识集)