iOS开发——记录一些好用的方法

前言

记录常用到的一些方法,持续更新。
2020年8月1日更新
2020年8月11日更新
2020年9月18日更新

1、数组

1.1 查找数组中是否含有某个对象

// myArr是自己的数组,object是要找的那个对象
[myArr containsObject:object];

1.2 NSMutableArray initWithCapacity 的作用和区别

1,initWithCapacity:10 并不代表里面的object数量不能大于10.也可以大于10.
2,init是在告诉程序,“我不知道要放多少object,暂且帮我初始化”。
3,如果你知道大概要放多少东西,那么最好用initWithCapacity,这个会提高程序内存运用效率。
4,如果你初始化了NSMutableArray,并且很长时间不会用到,建议用initWithCapacity:0。

2、字典

2.1 新建key-val

[myDic setValue: forKey:];

2.2 根据key查value

[myDic valueForKey: ];

2.3 字典添加object-key

[myDic setObject:object forKey:@"key"];

2.4 判定字典中是否含有某个key

if([dic objectForKey:@"key"]){

}

3、字符串

3.1 字符串按字符拆分为数组

// 以”/“划分
NSArray *stringURLArray = [myString componentsSeparatedByString:@"/"];

4、布局

4.1 frame与bounds的区别详解

frame与bounds的区别详解

4.2 获取当前屏幕尺寸

// iOS10以后
#define creenWidth [UIApplication sharedApplication].windows[0].bounds.size.width
#define creenHeight [UIApplication sharedApplication].windows[0].bounds.size.height

4.3 隐藏任务栏

iOS9.0以后废弃,记录一下。[[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone];

// 新方式
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self setNeedsStatusBarAppearanceUpdate];
}
- (BOOL)prefersStatusBarHidden{
    return YES;
}

4.4 隐藏/显示导航栏

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:animated];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}

5、UI

5.1 UICollectionView性能优化

5.1.1 iOS10的新特性

主要体现在:

  1. 顺滑的滑动体验(最好用)
  2. 针对self-sizing的改进
  3. Interactive reordering重排
    详细介绍请参考iOS 10 UICollectionView 性能优化
    文章不错,很详细,还介绍了cell的各个阶段的状态,值得收藏,亲测很有效果,不再赘述。
// 预加载代理
- (void)collectionView:(UICollectionView *)collectionView prefetchItemsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths{
   	NSLog(@"--预加载, index.count = %lu", (unsigned long)indexPaths.count);
    for (NSIndexPath *index in indexPaths){
        NSLog(@"--index = %li", (long)index.item);
    }
}

5.1.2 滑动消失cell

- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath{
	// 可以取到即将从屏幕消失的cell的indexPath,以及对cell做操作
//    cell.backgroundColor = [UIColor redColor];
}

5.1.3 滑动出现cell

- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath{
	// 同上
}

5.1.4 cell注册(复用)

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor whiteColor];
    return cell;
}

5.1.5 跳至指定位置

NSIndexPath *idxPath = [NSIndexPath indexPathForItem:1 inSection:0];
[self.collectionView scrollToItemAtIndexPath:idxPath atScrollPosition:0 animated:NO];

5.2 UILable相关

5.2.1 文字居中

label.textAlignment = UITextAlignmentLeft;         // 左对齐

label.textAlignment = UITextAlignmentCenter;     // 居中对齐

label.textAlignment = UITextAlignmentRight;        // 右对齐

5.2.1 跟随文本重置自己宽高

设置lable控件随文本长度变换,并且位于屏幕中央。

// 预先给出一个位置
self.titleLable = [[UILabel alloc] initWithFrame:frame];
self.titleLable.text = @"你的内容";
// 设置label可以有多行
self.titleLabel.numberOfLines = 0;
// 最重要的一句
[self.titleLabel sizeToFit];
// 重置位置
self.titleLabel.frame = CGRectMake(centerX - self.titleLabel.frame.size.width / 2, centerY + 12, self.titleLabel.frame.size.width, self.titleLabel.frame.size.height);

5.3 UIScrollView相关

5.3.1 滑动停止时候的操作

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
	// 相应操作
}

5.3.2 滑动过程中的操作

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    //获取处于UITableView中心的cell
    //系统方法返回处于tableView某坐标处的cell的indexPath
    NSIndexPath *middleIndexPath = [self.tableView  indexPathForRowAtPoint:CGPointMake(0, scrollView.contentOffset.y + self.tableView.frame.size.height / 2)];    
    NSLog(@"当前的位置:middleIndexPath = %lu", middleIndexPath.row);
}

5.4 UITableView

5.4.1 cell注册

static NSString *personTableIdentifier = @"normalCell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:personTableIdentifier];
if(cell == nil)
{
	cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:personTableIdentifier];
}

5.4.2 关闭滑动条

collectionView有一样的属性

    self.tableView.showsVerticalScrollIndicator = NO;

5.5 关于statusBar、navBar的几处笔记

关于statusBar、navBar的几处笔记

6、MJRefrsh

- (void)refreshConfig{
     // 刷新方法
     MJRefreshGifHeader *header = [MJRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewDataUp)];
     MJRefreshAutoGifFooter *footer = [MJRefreshAutoGifFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadNewDataDown)];
    
     // 设置文字
     [header setTitle:@"下拉刷新" forState:MJRefreshStateIdle];
     [header setTitle:@"松手即可刷新" forState:MJRefreshStatePulling];
     [header setTitle:@"加载中" forState:MJRefreshStateRefreshing];
    
//     [footer setTitle:@"上滑加载更多" forState:MJRefreshStateIdle];
     [footer setTitle:@"松手即可加载" forState:MJRefreshStatePulling];
     [footer setTitle:@"加载中" forState:MJRefreshStateRefreshing];
    
     // 设置字体
     header.stateLabel.font = [UIFont systemFontOfSize:15];
     header.lastUpdatedTimeLabel.font = [UIFont systemFontOfSize:14];
    
    footer.stateLabel.text = @"上滑加载更多";
    footer.stateLabel.font = [UIFont systemFontOfSize:15];

     // 设置颜色
     header.stateLabel.textColor = [UIColor redColor];
     header.lastUpdatedTimeLabel.textColor = [UIColor blueColor];
     // 设置普通状态的动画图片
     [header setImages:[self getRefreshingImageArrayWithStartIndex:1 endIndex:4] forState:MJRefreshStateIdle];
     // 设置即将刷新状态的动画图片(一松开就会刷新的状态)
     [header setImages:[self getRefreshingImageArrayWithStartIndex:5 endIndex:10] forState:MJRefreshStatePulling];
     // 设置正在刷新状态的动画图片
     [header setImages:[self getRefreshingImageArrayWithStartIndex:10 endIndex:26] forState:MJRefreshStateRefreshing];
     
     // 隐藏时间
//     header.lastUpdatedTimeLabel.hidden = YES;
     // 隐藏状态
//     header.stateLabel.hidden = YES;
     
     self.tableView.mj_header = header;
     self.tableView.mj_footer = footer;
}
// 下拉刷新
- (void)loadNewDataUp{
    [self.adViewArray removeAllObjects];
    [self.adsArray removeAllObjects];
    [self pressToLoadAd];
    NSLog(@"下拉刷新");
}

// 上拉刷新
- (void)loadNewDataDown{
    [self pressToLoadAd];
    NSLog(@"上拉加载");
}

// 停止刷新
- (void)endRefresh{
    [self.tableView.mj_footer endRefreshing];
    [self.tableView.mj_header endRefreshing];
}
// gif相关
- (NSArray *)getRefreshingImageArrayWithStartIndex:(NSInteger)startIndex endIndex:(NSInteger)endIndex{
    
    NSMutableArray *result = [NSMutableArray array];
    for (NSUInteger i = startIndex; i <= endIndex; i++) {
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"chewImage.bundle/%lu@2x.jpg", (unsigned long)i]];
        if (image) {
            NSLog(@"加载图片chewImage.bundle/%lu", (unsigned long)i);
            [result addObject:image];
        }
    }
    return result;
    
}

7. 计算某个代码块运行时长

NSDate* tmpStartData = [NSDate date];
double deltaTime = [[NSDate date] timeIntervalSinceDate:tmpStartData];
NSLog(@">>>>>>>>>>xincost time = %f ms", deltaTime*1000);

8. NSURLSession普通数据下载

    NSURLSession *session = [NSURLSession sharedSession];

    NSDate* tmpStartData1 = [NSDate date];

    NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:self.currentRequest completionHandler:^(NSData *data, NSURLResponse * response, NSError *error) {
    });

9. delegate调用respondsToSelector方法失败

// 错误信息为
no known instance method for selector 'respondsToSelector'

因为respondsToSelector方法是NSObject的一个实例方法。
所以可以写成

[(NSObject*)self.delegate respondsToSelector:@selector(myClass:willDoSomething:)]

10. 彻底删除xcode

xcode删除后,再从appstore下载报错:可用的磁盘空间不足,无法安装此产品。

1.根目录下的必须要用管理员权限:
sudo rm -rf /Applications/Xcode.app
sudo rm -rf /Library/Preferences/com.apple.dt.Xcode.plist

2.然后删除自己目录下的:
rm -rf ~/Library/Preferences/com.apple.dt.Xcode.plist
rm -rf ~/Library/Caches/com.apple.dt.Xcode
rm -rf ~/Library/Application\ Support/Xcode(即Application Support)

3.以及和开发者相关的:
默认Xcode是安装在磁盘的根目录上,目录名:Developer
rm -rf ~/Library/Developer/Xcode
rm -rf ~/Library/Developer/CoreSimulator
rm -rf ~/Library/Developer/XCPGDevices

4.xcode缓存目录
rm -rf ~/Library/Developer/Xcode/DerivedData

5.Xcode余下的一些旧标记 Development Tools
sudo /Developer/Library/uninstall-devtools –mode=all

11. iOS的UI好看的学习地址

http://code.cocoachina.com/list/31/9
特别多

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
iOS开发中,有许多常用的库和框架可以帮助开发者提高效率和加速开发过程。以下是一些常用的库和框架: 1. Alamofire:一个简洁的网络请求库,提供了一种优雅的方式来进行网络请求和处理响应。 2. SDWebImage:一个用于异步加载和缓存网络图片的库,可以帮助提高图片加载性能,并且具有内存和磁盘缓存机制。 3. AlamofireImage:一个基于Alamofire的图片加载库,提供了一些便捷的方法来加载网络图片并进行缓存。 4. SwiftyJSON:一个轻量级的、灵活的JSON解析库,可以帮助简化处理JSON数据的过程。 5. SnapKit:一个优雅的、轻量级的Auto Layout框架,使用Swift语言提供了一种简化UI布局代码的方式。 6. Realm:一个移动数据库框架,提供了高效的数据存储和查询功能,并且支持对象关系映射(ORM)。 7. AlamofireObjectMapper:一个将Alamofire与ObjectMapper结合使用的库,可以方便地将JSON数据映射到模型对象中。 8. Kingfisher:一个用于异步加载和缓存网络图片的库,具有高性能和功能丰富的特点。 9. RxSwift:一个用于响应式编程的库,可以简化异步编程和事件处理的复杂性。 10. IQKeyboardManager:一个用于处理键盘弹出和收起的库,可以自动管理键盘,提供了一种简单的方式来避免键盘遮挡输入框的问题。 这只是一小部分常用的库和框架,iOS开发中还有许多其他优秀的工具可供选择,根据具体需求选择合适的库和框架进行开发

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值