文章目录
前言
记录常用到的一些方法,持续更新。
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的区别详解
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的新特性
主要体现在:
- 顺滑的滑动体验(最好用)
- 针对self-sizing的改进
- 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的几处笔记
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
特别多