IOS开发基础知识--碎片40
1:Masonry快速查看报错小技巧
self.statusLabel = [UILabel new];
[self.contentView addSubview:self.statusLabel];
MASAttachKeys(self.statusLabel); [self.statusLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.contentView).offset(15.0f);
make.left.equalTo(self.contentView).offset(.f);
make.height.equalTo();
make.bottom.equalTo(self.contentView);
}];
注意:MASAttachKeys会显示出比较明了的错误信息;
2:iOS跳转到系统设置
注意:想要实现应用内跳转到系统设置界面功能,需要先在Targets-Info-URL Types-URL Schemes中添加prefs
跳转到WIFI设置
if ([[UIApplication sharedApplication]
canOpenURL:[NSURL URLWithString:@"prefs:root=WIFI"]])
{
[[UIApplication sharedApplication]
openURL:[NSURL URLWithString:@"prefs:root=WIFI"]];
} 跳转到蓝牙
if ([[UIApplication sharedApplication]
canOpenURL:[NSURL URLWithString:@"prefs:root=Bluetooth"]])
{
[[UIApplication sharedApplication]
openURL:[NSURL URLWithString:@"prefs:root=Bluetooth"]];
} 跳转到通用
if ([[UIApplication sharedApplication]
canOpenURL:[NSURL URLWithString:@"prefs:root=General"]])
{
[[UIApplication sharedApplication]
openURL:[NSURL URLWithString:@"prefs:root=General"]];
} 跳转到关于本机
if ([[UIApplication sharedApplication]
canOpenURL:[NSURLURLWithString:
@"prefs:root=General&path=About"]])
{
[[UIApplication sharedApplication]
openURL:[NSURL URLWithString:@"prefs:
root=General&path=About"]];
} 跳转到定位服务
if ([[UIApplication sharedApplication]
canOpenURL:[NSURL URLWithString:@"prefs:
root=LOCATION_SERVICES"]])
{
[[UIApplication sharedApplication]
openURL:[NSURL URLWithString:@"prefs:
root=LOCATION_SERVICES"]];
} 跳转到通知
if ([[UIApplication sharedApplication]
canOpenURL:[NSURL URLWithString:@"prefs:
root=NOTIFICATIONS_ID"]])
{
[[UIApplication sharedApplication]
openURL:[NSURL URLWithString:@"prefs:
root=NOTIFICATIONS_ID"]];
}
3:UITableView section随着cell滚动
实现UITableView 的下面这个方法,
#pragma mark - Scroll
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
CGFloat sectionHeaderHeight = ;
//固定section 随着cell滚动而滚动
if (scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=) { scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, , , ); } else if (scrollView.contentOffset.y>=sectionHeaderHeight) { scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, , , ); } }
4:TableView如何刷新指定的cell 或section
//一个section刷新 NSIndexSet *indexSet=[[NSIndexSet alloc]initWithIndex:]; [tableview reloadSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic]; //一个cell刷新 NSIndexPath *indexPath=[NSIndexPath indexPathForRow: inSection:]; [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation:UITableViewRowAnimationNone];
5:TableView另一种实现隔行空白的效果
增加总体条数,然后再求余判断
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CELL_ID2 = @"SOME_STUPID_ID2";
// even rows will be invisible
if (indexPath.row % == )
{
UITableViewCell * cell2 = [tableView dequeueReusableCellWithIdentifier:CELL_ID2];
if (cell2 == nil)
{
cell2 = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CELL_ID2];
[cell2.contentView setAlpha:];
[cell2 setUserInteractionEnabled:NO]; // prevent selection and other stuff
}
return cell2;
}
[ccTableView setBackgroundColor:[UIColor clearColor]];
cardsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cardsCell"];
if(cell == nil){
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"cardsCell" owner:self options:nil];
cell = [topLevelObjects objectAtIndex:];
}
// Use indexPath.row/2 instead of indexPath.row for the visible section to get the correct datasource index (number of rows is increased to add the invisible rows)
NSString *nmCard = [[self.cards valueForKeyPath:@"cards.name"] objectAtIndex:(indexPath.row/)];
cell.descCardLabel.text = nmCard;
return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
// two times minus one (invisible at even rows => visibleCount == invisibleCount+1)
return [[self.cards valueForKeyPath:@"cards"] count] * - ;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row % == )
return ;
return ;
}
6:滚动TableView,滚动到指定的位置
- (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated
typedef enum {
UITableViewScrollPositionNone,
UITableViewScrollPositionTop,
UITableViewScrollPositionMiddle,
UITableViewScrollPositionBottom
} UITableViewScrollPosition;
7:iOS-检测UI主线程小工具
在iOS开发中需要保证所有UI操作一定是在主线程进行,通过 hook UIView的-setNeedsLayout,-setNeedsDisplay,-setNeedsDisplayInRect三个方法,确保它们都是在主线程执行。
#import "UIView+NBUIKitMainThreadGuard.h"
#import <objc/runtime.h> static inline void swizzling_exchangeMethod(Class clazz ,SEL originalSelector, SEL swizzledSelector){
Method originalMethod = class_getInstanceMethod(clazz, originalSelector);
Method swizzledMethod = class_getInstanceMethod(clazz, swizzledSelector); BOOL success = class_addMethod(clazz, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod));
if (success) {
class_replaceMethod(clazz, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));
} else {
method_exchangeImplementations(originalMethod, swizzledMethod);
}
} @implementation UIView (NBUIKitMainThreadGuard) +(void)load
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{ SEL needsLayoutOriginalSelector = @selector(setNeedsLayout);
SEL needsLayoutSwizzleSelector = @selector(guard_setNeedsLayout);
swizzling_exchangeMethod(self, needsLayoutOriginalSelector,needsLayoutSwizzleSelector); SEL needsDisplaOriginalSelector = @selector(setNeedsDisplay);
SEL needsDisplaSwizzleSelector = @selector(guard_setNeedsDisplay);
swizzling_exchangeMethod(self, needsDisplaOriginalSelector,needsDisplaSwizzleSelector); SEL needsDisplayInRectOriginalSelector = @selector(setNeedsDisplayInRect:);
SEL needsDisplayInRectSwizzleSelector = @selector(guard_setNeedsDisplayInRect:);
swizzling_exchangeMethod(self, needsDisplayInRectOriginalSelector,needsDisplayInRectSwizzleSelector); });
} - (void)guard_setNeedsLayout
{
[self UIMainThreadCheck];
[self guard_setNeedsLayout];
} - (void)guard_setNeedsDisplay
{
[self UIMainThreadCheck];
[self guard_setNeedsDisplay];
} - (void)guard_setNeedsDisplayInRect:(CGRect)rect
{
[self UIMainThreadCheck];
[self guard_setNeedsDisplayInRect:rect];
} - (void)UIMainThreadCheck
{
NSString *desc = [NSString stringWithFormat:@"%@", self.class];
NSAssert([NSThread isMainThread], desc);
} @end
IOS开发基础知识--碎片40的更多相关文章
- IOS开发基础知识碎片-导航
1:IOS开发基础知识--碎片1 a:NSString与NSInteger的互换 b:Objective-c中集合里面不能存放基础类型,比如int string float等,只能把它们转化成对象才可 ...
- IOS开发基础知识--碎片33
1:AFNetworking状态栏网络请求效果 直接在AppDelegate里面didFinishLaunchingWithOptions进行设置 [[AFNetworkActivityIndicat ...
- IOS开发基础知识--碎片42
1:报thread 1:exc_bad_access(code=1,address=0x70********) 闪退 这种错误通常是内存管理的问题,一般是访问了已经释放的对象导致的,可以开启僵尸对象( ...
- IOS开发基础知识--碎片50
1:Masonry 2个或2个以上的控件等间隔排序 /** * 多个控件固定间隔的等间隔排列,变化的是控件的长度或者宽度值 * * @param axisType 轴线方向 * @param fi ...
- IOS开发基础知识--碎片3
十二:判断设备 //设备名称 return [UIDevice currentDevice].name; //设备型号,只可得到是何设备,无法得到是第几代设备 return [UIDevice cur ...
- IOS开发基础知识--碎片11
1:AFNetwork判断网络状态 #import “AFNetworkActivityIndicatorManager.h" - (BOOL)application:(UIApplicat ...
- IOS开发基础知识--碎片14
1:ZIP文件压缩跟解压,使用ZipArchive 创建/添加一个zip包 ZipArchive* zipFile = [[ZipArchive alloc] init]; //次数得zipfilen ...
- IOS开发基础知识--碎片16
1:Objective-C语法之动态类型(isKindOfClass, isMemberOfClass,id) 对象在运行时获取其类型的能力称为内省.内省可以有多种方法实现. 判断对象类型 -(BOO ...
- IOS开发基础知识--碎片19
1:键盘事件顺序 UIKeyboardWillShowNotification // 键盘显示之前 UIKeyboardDidShowNotification // 键盘显示完成后 UIKeyboar ...
随机推荐
- C++中public、protected及private用法
转自:http://www.jb51.net/article/54224.htm 初学C++的朋友经常在类中看到public,protected,private以及它们在继承中表示的一些访问范围,很容 ...
- HTTP的长连接和短连接——Node上的测试
本文主要从实践角度介绍长.短连接在TCP层面的表现,借助Node.JS搭建后台服务,使用WinHTTP.Ajax做客户端请求测试,最后简单涉及WebSocket. 关键字:长连接.短连 ...
- Lua 学习笔记(七)编译、执行外部代码块
Lua称为解释型语言的原因:Lua允许在运行源代码之前,先将源代码预编译为一种中间形式.区别解释型语言的主要特征是在于编译器是否是语言运行时库的一部分,即有能力执行动态生成的代码.因为Lua中有dof ...
- java基础--java.util.Date类型小结
首先先来了解一下Date数据类型: . Date类型通常要和另一个 java.text.SimpleDateFormat类联合使用. 把long-->Date: public Date(long ...
- 【JavaScript】又一神器框架:linq.js
引言 前几天针对一个js数组交集的问题请教了下同事,他第一反应就是循环,这也是常规思路,因为我个人更倾向于js些,我便开玩笑,不知道js能不能像linq那样实现这些操作呢?果断百度了一把,果然有现成框 ...
- 用Vagrant和Ansible搭建持续交付平台
这是一个关于Vagrant的学习系列,包含如下文章: Vagrant入门 创建自己的Vagrant box 用Vagrant搭建Jenkins构建环境 用Vagrant和Ansible搭建持续交付平台 ...
- 关于实现手机端自动获取天气的demo
博主大二做的一个项目,当时很傻很天真,但是还是贴出来,希望能给大家一点帮助.欢迎转载哦!我的博客园地址:http://www.cnblogs.com/natureless/ 首先分析需求,移动端实现天 ...
- Suggestion(搜索建议)产品和技术
今天来简单聊聊Suggestion产品 什么是Suggestion服务? 一图胜千言: 当你想要搜索某个长词语或者一句话输入部分时,Suggestion服务预测你极大可能的候选项,并罗列出来,供你选择 ...
- CSS3学习总结
(写在前边~)过了这么长时间,才写博客,最近实在是太忙了,做了两个项目,一个项目用于练手,自我感觉做的很漂亮,一个项目是一套完整的页面体系,答辩时候老师给了100分和99分,并让我打包上传,我很是感动 ...
- [译]Godot系列教程三 - 场景实例化(续)
场景实例化(续) 要点 场景实例化带来很多便利的用法,总体来说有: 将场景细分,更便于管理 相对于某些引擎中的Prefab组件更灵活,并且在许多方面更强大 是一种设计更复杂的游戏流程甚至UI的方式 这 ...