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 ...
随机推荐
- 清除页面广告?身为前端,自己做一款简易的chrome扩展吧
大家肯定有这样的经历,浏览网页的时候,左右两端广告,诸如“屠龙宝刀,点击就送”,以及最近火的不行的林子聪37传奇霸业什么“霸业面具,霸业吊坠”的魔性广告总是充斥我们的眼球. 当然有现成的扩展程序或者插 ...
- 【转】java中注解的使用与实例
原文:http://www.cnblogs.com/pepcod/archive/2013/02/20/2918719.html java 注解,从名字上看是注释,解释.但功能却不仅仅是注释那么简单. ...
- canvas学习笔记
html5的新标签:canvas; 作用:标签定义图形,比如图表和其他图像:标签只是图形容器,您必须使用脚本来绘制图形.默认大小:宽300px,高150px; 背景知识:概念最初由苹果公司提出的,用于 ...
- 小菜学习设计模式(四)—原型(Prototype)模式
前言 设计模式目录: 小菜学习设计模式(一)—模板方法(Template)模式 小菜学习设计模式(二)—单例(Singleton)模式 小菜学习设计模式(三)—工厂方法(Factory Method) ...
- 开发中 常用 js 记录(一)
(1)获得URL参数 function GetQueryString("url参数名") { var reg = new RegExp("(^|&)&qu ...
- mininet和ryu控制器的连接
1.执行ryu应用程式:ryu-manager --verbose ryu.app.simple_switch_13 2.启动mininet,配置如下:创建3个host,1个交换器(open vSwi ...
- jQuery-1.9.1源码分析系列(十) 事件系统——事件体系结构
又是一个重磅功能点. 在分析源码之前分析一下体系结构,有助于源码理解.实际上在jQuery出现之前,Dean Edwards的跨浏览器AddEvent()设计做的已经比较优秀了:而且jQuery事件系 ...
- 那些年我们用过的Python代码
根据正则表达式选择性解压文件 from zipfile import ZipFile import re def is_match(expression, filename): pattern = r ...
- 写出好的 commit message
为何要关注提交信息 加快Reviewing Code的过程 帮助我们写好release note 5年后帮你快速想起来某个分支,tag或者 commit增加了什么功能,改变了哪些代码 让其他的开发者在 ...
- Effective java笔记(六),方法
38.检查参数的有效性 绝大多数方法和构造器对于传递给它们的参数值都会有限制.如,对象引用不能为null,数组索引有范围限制等.应该在文档中指明所有这些限制,并在方法的开头处检查参数,以强制施加这些限 ...